public ReflectedType(Type type) { UnderlyingType = type; TypeName = type.GetFriendlyName(); FullTypeName = type.FullName; var interfaceType = Reflector.GetInterface(type); if (interfaceType != null) { InterfaceType = interfaceType; InterfaceTypeName = interfaceType.FullName; } IsArray = type.IsArray; IsList = Reflector.IsList(type); IsDictionary = Reflector.IsDictionary(type); IsSimpleList = Reflector.IsSimpleList(type); IsDataEntity = Reflector.IsDataEntity(type) || (!Reflector.IsSimpleType(type) && !IsArray && !IsList && !IsDictionary); Type elementType; IsDataEntityList = Reflector.IsDataEntityList(type, out elementType); ElementType = elementType; if (IsDataEntityList) { IsPolymorphicList = elementType != null && elementType.IsAbstract && (!elementType.IsInterface || !Reflector.IsDataEntity(elementType)); IsListInterface = type.GetGenericTypeDefinition() == typeof(IList <>); } IsSimpleType = Reflector.IsSimpleType(type); IsNullableType = Reflector.IsNullableType(type); IsMarkerInterface = Reflector.IsMarkerInterface(type); HashCode = type.GetHashCode(); IsGenericType = type.IsGenericType; IsInterface = type.IsInterface; IsAnonymous = Reflector.IsAnonymousType(type); IsEmitted = Reflector.IsEmitted(type); }
public ReflectedProperty(PropertyInfo property, int position = -1, bool readAttributes = true) { PropertyName = property.Name; PropertyType = property.PropertyType; IsPersistent = true; IsSelectable = true; IsSerializable = true; IsBinary = property.PropertyType == typeof(byte[]); IsSimpleList = !IsBinary && Reflector.IsSimpleList(property.PropertyType); IsDataEntity = Reflector.IsDataEntity(property.PropertyType); Type elementType; IsDataEntityList = Reflector.IsDataEntityList(property.PropertyType, out elementType); ElementType = elementType; IsPolymorphicList = IsDataEntityList && elementType != null && elementType.IsAbstract && !elementType.IsInterface; if (IsDataEntityList) { IsList = true; IsListInterface = property.PropertyType.GetGenericTypeDefinition() == typeof(IList <>); } else { IsList = !IsBinary && Reflector.IsList(property.PropertyType); if (IsList) { ElementType = Reflector.GetElementType(property.PropertyType); IsListInterface = property.PropertyType.GetGenericTypeDefinition() == typeof(IList <>); } } IsSimpleType = !IsBinary && Reflector.IsSimpleType(property.PropertyType); IsNullableType = Reflector.IsNullableType(property.PropertyType); CanWrite = property.CanWrite; CanRead = property.CanRead; Position = position; IsObject = IsDataEntity || (property.PropertyType.IsClass && !IsSimpleType && !IsSimpleList && !IsBinary && !IsList); IsObjectList = IsDataEntityList || (IsList && ElementType != null && ElementType.IsClass && !Reflector.IsSimpleType(ElementType) && !Reflector.IsSimpleList(ElementType)); Converter = null; MappedColumnName = property.Name; if (readAttributes) { if (IsListInterface) { Sorted = property.GetCustomAttributes(typeof(SortedAttribute), false).Cast <SortedAttribute>().FirstOrDefault(); Distinct = property.GetCustomAttributes(typeof(DistinctAttribute), false).Cast <DistinctAttribute>().FirstOrDefault(); } MappedColumnName = MapColumnAttribute.GetMappedColumnName(property); MappedPropertyName = MapPropertyAttribute.GetMappedPropertyName(property); var typeConverter = TypeConverterAttribute.GetTypeConverter(property); if (typeConverter != null) { AddConverter(typeConverter.TypeConverterType); } var items = property.GetCustomAttributes(true).OfType <PropertyAttribute>(); foreach (var item in items) { var primaryKeyAttribute = item as PrimaryKeyAttribute; if (primaryKeyAttribute != null) { IsPrimaryKey = true; KeyPosition = primaryKeyAttribute.Position; } else { var generategAttribute = item as Generate.UsingAttribute; if (generategAttribute != null) { Generator = generategAttribute.Type; } else if (item is Generate.NativeAttribute) { IsAutoGenerated = true; } else { var referencesAttribute = item as ReferencesAttribute; if (referencesAttribute != null) { Parent = referencesAttribute.Parent; RefPosition = referencesAttribute.Position; } else { var parameterAttribute = item as ParameterAttribute; if (parameterAttribute != null) { ParameterName = parameterAttribute.Name; Direction = parameterAttribute.Direction; } else if (item is DoNotPersistAttribute) { IsPersistent = false; } else if (item is DoNotSelectAttribute) { IsSelectable = false; } else if (item is DoNotSerializeAttribute) { IsSerializable = false; } } } } } } }
private static void DefineGuardedProperty(PropertyInfo property, TypeBuilder typeBuilder, FieldInfo field, Type objectType, bool ignoreMappings, IEntityMap entityMap) { // create the new property. var propertyBuilder = typeBuilder.DefineProperty(property.Name, System.Reflection.PropertyAttributes.HasDefault, property.PropertyType, null); // The property "set" and property "get" methods require a special set of attributes. //var getSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig | MethodAttributes.Virtual; var getSetAttr = MethodAttributes.Private | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual | MethodAttributes.Final | MethodAttributes.SpecialName; var columnName = MappingFactory.GetPropertyOrColumnName(property, ignoreMappings, entityMap, true); // create the getter if we can read. if (property.CanRead) { // create the get method for the property. var getMethodName = "get_" + property.Name; var getMethod = typeBuilder.DefineMethod(getMethodName, getSetAttr, property.PropertyType, Type.EmptyTypes); // get the IL generator to generate the required IL. ILGenerator il = getMethod.GetILGenerator(); // directly call the inner object's get method of the property. Type elementType; if (Reflector.IsDataEntityList(property.PropertyType, out elementType)) { var isInterface = property.PropertyType.GetGenericTypeDefinition() == typeof(IList <>); var asReadOnlyMethod = typeof(ObjectExtensions).GetMethods(BindingFlags.Public | BindingFlags.Static).Where(m => m.Name == "AsReadOnly").First(m => m.GetParameters()[0].ParameterType.Name == (isInterface ? "IList`1" : "List`1")); var result = il.DeclareLocal(property.PropertyType); // load the first argument (the instance itself) and the field. il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldfld, field); il.EmitCall(OpCodes.Callvirt, property.GetGetMethod(), null); il.Emit(OpCodes.Call, asReadOnlyMethod.MakeGenericMethod(elementType)); il.Emit(OpCodes.Stloc_0, result); il.Emit(OpCodes.Ldloc_0); } else if (Reflector.IsDataEntity(property.PropertyType)) { var asReadOnlyMethod = typeof(ObjectExtensions).GetMethods(BindingFlags.Public | BindingFlags.Static).Where(m => m.Name == "AsReadOnly").First(m => m.GetParameters()[0].ParameterType.IsGenericParameter); var result = il.DeclareLocal(property.PropertyType); // load the first argument (the instance itself) and the field. il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldfld, field); il.EmitCall(OpCodes.Callvirt, property.GetGetMethod(), null); il.Emit(OpCodes.Call, asReadOnlyMethod.MakeGenericMethod(property.PropertyType)); il.Emit(OpCodes.Stloc_0, result); il.Emit(OpCodes.Ldloc_0); } else if (property.DeclaringType.InheritsFrom(typeof(ITrackableDataEntity)) && property.PropertyType == typeof(ObjectState) && property.Name == "ObjectState") { il.Emit(OpCodes.Ldc_I4, (int)ObjectState.ReadOnly); } else { // load the first argument (the instance itself) and the field. il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldfld, field); il.EmitCall(OpCodes.Callvirt, property.GetGetMethod(), null); } il.Emit(OpCodes.Ret); // set the method. propertyBuilder.SetGetMethod(getMethod); typeBuilder.DefineMethodOverride(getMethod, property.ReflectedType.GetMethod(getMethodName)); } // create the setter if we can read. if (property.CanWrite) { // create the set method of the property. var setMethodName = "set_" + property.Name; var setMethod = typeBuilder.DefineMethod(setMethodName, getSetAttr, null, new Type[] { property.PropertyType }); // get the IL generator to generate some IL. ILGenerator il = setMethod.GetILGenerator(); // load the first argument (instance itself) and the field. il.Emit(OpCodes.Newobj, typeof(NotSupportedException).GetConstructor(Type.EmptyTypes)); il.Emit(OpCodes.Throw); propertyBuilder.SetSetMethod(setMethod); typeBuilder.DefineMethodOverride(setMethod, property.ReflectedType.GetMethod(setMethodName)); } }