// ctor. public OrmProperty(MemberInfo memberInfo) { Debug.Assert(memberInfo != null); PropertyName = memberInfo.Name; MemberType = memberInfo.GetMemberType(); var attribute = memberInfo.GetCustomAttribute <TypeConverterAttribute>(); if (attribute != null) { if (Type.GetType(attribute.ConverterTypeName) is Type converterType) { TypeConverter = StaticCache.TypeConverters.GetOrAdd(converterType, ConverterValueFactory); } else { throw new MicroOrmException($"Unable to resolve converter type {attribute.ConverterTypeName}"); } } if (DynamicReflectionDelegateFactory.CreateSet <object>(memberInfo) is { } setValueDelegate) { SetValueHandler = new SetValueDelegate(setValueDelegate); } IsNonNullable = NonNullableConvention.IsNonNullableReferenceType(memberInfo); }
///// <summary> ///// Конструктор анонимного типа. ///// </summary> //private static Dictionary<string, ConstructorArgument> CreateConstructorArguments(PropertyInfo[] properties) //{ // return properties // .Select((x, Index) => new { PropertyInfo = x, Index }) // .ToDictionary(x => x.PropertyInfo.Name, x => new ConstructorArgument(x.Index, x.PropertyInfo)); //} private static void InitializeStreamingMethods(Type type, out OnDeserializingDelegate?onDeserializingHandle, out OnDeserializedDelegate?onDeserializedHandle) { onDeserializingHandle = null; onDeserializedHandle = null; MethodInfo[] methods = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly); for (int i = 0; i < methods.Length; i++) { MethodInfo method = methods[i]; if (method.IsDefined(typeof(OnDeserializingAttribute), false)) { onDeserializingHandle = DynamicReflectionDelegateFactory.CreateOnDeserializingMethodCall(method, type); } if (method.IsDefined(typeof(OnDeserializedAttribute), false)) { onDeserializedHandle = DynamicReflectionDelegateFactory.CreateOnDeserializedMethodCall(method, type); } } }
private static TypeConverter ConverterValueFactory(Type converterType) { var ctor = DynamicReflectionDelegateFactory.CreateDefaultConstructor <TypeConverter>(converterType); return(ctor.Invoke()); }
// ctor. // Мы защищены от одновременного создания с помощью Lazy.ExecutionAndPublication. public ContractActivator(Type dboType) { if (SingleNonEmptyCtor(dboType, out ConstructorInfo? singleCtor)) { IsEmptyCtor = false; _ctorActivator = DynamicReflectionDelegateFactory.CreateConstructor(dboType, singleCtor); ConstructorArguments = CreateConstructorArguments(singleCtor); } else { // Хоть у структуры и есть пустой конструктор, нам он не подходит. IsEmptyCtor = true; _emptyCtorActivator = DynamicReflectionDelegateFactory.CreateDefaultConstructor <object>(dboType); } InitializeStreamingMethods(dboType, out OnDeserializingHandle, out OnDeserializedHandle); //if (!anonymousType) //{ // // Тип может быть readonly структурой, определить можно только перебором всех свойств и полей. // bool isReadonlyStruct = GetIsReadonlyStruct(dboType); // if (isReadonlyStruct) // { // // Хоть у структуры и есть пустой конструктор, нам он не подходит. // IsEmptyCtor = false; // ConstructorInfo[] ctors = dboType.GetConstructors(); // Debug.Assert(ctors.Length > 0, "У типа должны быть открытые конструкторы"); // if (ctors.Length != 0) // { // ConstructorInfo ctor = ctors[0]; // _ctorActivator = DynamicReflectionDelegateFactory.CreateConstructor(dboType, ctor); // ConstructorArguments = CreateConstructorArguments(ctor); // } // else // throw new MicroOrmException("Не найден открытый конструктор"); // InitializeStreamingMethods(dboType, out OnDeserializingHandle, out OnDeserializedHandle); // } // else // // Не анонимный class // { // } //} //else { //_ctorActivator = DynamicReflectionDelegateFactory.CreateAnonimousConstructor(dboType); //// поля у анонимных типов не рассматриваются. //// берем только свойства по умолчанию. //PropertyInfo[] properties = dboType.GetProperties(BindingFlags.Public | BindingFlags.Instance); //// порядок полей такой же как у конструктора. //ConstructorArguments = CreateConstructorArguments(properties); } Contract = new TypeContract(dboType); }