OrientDB .Net 라이브러리에 일부 확장 메서드를 작성하고 데이터베이스의 C# 및 그래프 (및 그 반대로) 모델 클래스 간의 매핑 작업을하고 있습니다. 이것은 반드시 약간의 반영과 동적 프로그래밍이 필요합니다.동적 목록 <T>. throwing 추가 RuntimeBinderException
다음 방법은 정점 사이의 모서리를 나타내는 모델 오브젝트의 특성 값을 설정하는 데 사용됩니다. 예를 들어, 정점 A가 모서리 C가있는 여러 개의 정점 B에 연결되어있는 경우 A 모델의 유형은 List<B>
이고 B 모델의 속성은 A
(일대 다 관계) 일 수 있습니다. 모델의
private static void SetLinkedProperty(
ABaseModel parent, ABaseModel child, string className)
{
PropertyInfo[] properties = parent.GetType()
.GetProperties(BindingFlags.Public |
BindingFlags.Instance |
BindingFlags.SetProperty |
BindingFlags.GetProperty);
PropertyInfo propertySingle = properties
.Where(prop => IsModelProperty(prop, className)).SingleOrDefault();
PropertyInfo propertyCollection = properties
.Where(prop => IsModelCollectionProperty(prop, className)).SingleOrDefault();
if (propertySingle != null)
{
propertySingle.SetValue(parent, child);
}
if (propertyCollection != null)
{
dynamic propertyValue = propertyCollection.GetValue(parent);
if (propertyValue == null)
{
Type listOfT = typeof(List<>).MakeGenericType(
propertyCollection.PropertyType.GenericTypeArguments[0]);
IEnumerable collection = (IEnumerable)Activator.CreateInstance(listOfT);
propertyValue = collection;
propertyCollection.SetValue(parent, collection);
}
propertyValue.Add(child);
}
}
속성 때문에 속성 유형이 별명에 대한뿐만 아니라 여부 IsModelProperty
및 IsModelCollectionProperty
확인이 열거 형은 그들에게 DB와 C# 클래스 사이의 매핑을 돕기 위해 별칭을주는 속성을 가질 수있다. 내 코드를 실행하면
그러나, 나는 선 propertyValue.Add(child)
에서 RuntimeBinderException
을 받고 있어요 :
형 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException'의 처리되지 않은 예외가 OrientTest.exe
발생추가 정보 :에 가장 적합한 오버로드 된 메서드에 일치 'System.Collections.Generic.List.Add (OrientTest.Participant는)'의 시점에서 일부 잘못된 인수
있다 예외 :
parent
가OrientTest.Participant
className
인스턴스OrientTest.Employer
child
의 인스턴스는 "EmployerParticipant"(고용주와 참가자를 연결하는 에지 클래스의 이름이 DB에 함께 정점)이다properties
은 7 개의 요소를 포함하며, 각 요소는 하나의 속성에 해당합니다.Employer
propertySingle
은입니다.propertyCollection
는List<Participant>#Add(Participant)
가 유효하지 않은 인수가 왜 이해가 안List<Participant>
의 인스턴스는 해당 부동산 List<Participant> Participants
propertyValue
을 나타내지 만 dynamic
은 종종 이상한 일을한다.