public static PropertyInformation GetPropertyInfo(this PropertyInfo prop) { var dynProp = new PropertyInformation { PropertyName = prop.Name, PropertyType = prop.PropertyType, CustomAttributes = GetCustomAttributes(prop) }; return(dynProp); }
/// <summary> /// Sets the property value. /// </summary> /// <param name="property">The property.</param> private void SetPropertyValue(PropertyInformation property) { if (property == null) { throw new ArgumentNullException("property"); } var prop = mResult.GetType() .GetProperty(property.PropertyName, BindingFlags.Public | BindingFlags.Instance); if (null != prop && prop.CanWrite) { prop.SetValue(mResult, property.PropertyValue, null); } }
public void AddProperty(PropertyInformation property) { if (mClassProperties.Any(x => x.PropertyName == property.PropertyName)) { var prop = mClassProperties.FirstOrDefault(x => x.PropertyName == property.PropertyName && x.PropertyType == property.PropertyType); if (prop == null) { return; } prop.PropertyValue = property.PropertyValue; prop.CustomAttributes = property.CustomAttributes; } else { mClassProperties.Add(property); } }
/// <summary> /// Makes the get method. /// </summary> /// <param name="fieldBuilder">The field builder.</param> /// <param name="propertyInformation">The property information.</param> /// <returns></returns> private MethodBuilder MakeGetMethod(FieldBuilder fieldBuilder, PropertyInformation propertyInformation) { MethodBuilder getPropMthdBldr = mTypeBuilder.DefineMethod( "get_" + propertyInformation.PropertyName, MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig, propertyInformation.PropertyType, Type.EmptyTypes); ILGenerator getIl = getPropMthdBldr.GetILGenerator(); getIl.Emit(OpCodes.Ldarg_0); getIl.Emit(OpCodes.Ldfld, fieldBuilder); getIl.Emit(OpCodes.Ret); return(getPropMthdBldr); }
/// <summary> /// Gets the object properties. /// </summary> /// <param name="obj">The object.</param> /// <returns></returns> public static List <PropertyInformation> GetObjectProperties(this object obj) { var type = obj.GetType(); var props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); var dynProps = new List <PropertyInformation>(); foreach (var prop in props) { var dynProp = new PropertyInformation { PropertyName = prop.Name, PropertyType = prop.PropertyType, PropertyValue = prop.GetValue(obj, null), CustomAttributes = GetCustomAttributes(prop) }; dynProps.Add(dynProp); } return(dynProps); }
/// <summary> /// Creates the property. /// </summary> /// <param name="propertyInformation">The property information.</param> private void CreateProperty(PropertyInformation propertyInformation, TypeBuilder typeBuilder) { FieldBuilder fieldBuilder = typeBuilder.DefineField("_" + propertyInformation.PropertyName, propertyInformation.PropertyType, FieldAttributes.Private); PropertyBuilder propertyBuilder = typeBuilder.DefineProperty(propertyInformation.PropertyName, PropertyAttributes.None, propertyInformation.PropertyType, null); //Добавление аттрибут к свойству if (propertyInformation.CustomAttributes != null) { foreach (var cusAttr in propertyInformation.CustomAttributes) { AddCustomAttribute(propertyBuilder, cusAttr); } } propertyBuilder.SetGetMethod(MakeGetMethod(fieldBuilder, propertyInformation)); propertyBuilder.SetSetMethod(MakeSetMethod(fieldBuilder, propertyInformation)); }
/// <summary> /// Makes the set method. /// </summary> /// <param name="fieldBuilder">The field builder.</param> /// <param name="propertyInformation">The property information.</param> /// <returns></returns> private MethodBuilder MakeSetMethod(FieldBuilder fieldBuilder, PropertyInformation propertyInformation) { MethodBuilder setPropMthdBldr = mTypeBuilder.DefineMethod("set_" + propertyInformation.PropertyName, MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig, null, new[] { propertyInformation.PropertyType }); ILGenerator setIl = setPropMthdBldr.GetILGenerator(); Label modifyProperty = setIl.DefineLabel(); Label exitSet = setIl.DefineLabel(); setIl.MarkLabel(modifyProperty); setIl.Emit(OpCodes.Ldarg_0); setIl.Emit(OpCodes.Ldarg_1); setIl.Emit(OpCodes.Stfld, fieldBuilder); //if (fieldBuilder.FieldType.IsValueType) // setIl.Emit(OpCodes.Box, fieldBuilder.FieldType); setIl.Emit(OpCodes.Nop); setIl.MarkLabel(exitSet); setIl.Emit(OpCodes.Ret); return(setPropMthdBldr); }
private ConstructorInfo CreateOwnAttributeConstructor(PropertyAttributeInformation attr) { //return null; var typeSignature = attr.Name; //var an = new AssemblyName(typeSignature + ",Version=1.0.0.1"); ////генерация динамической сборки только с возможностью запуска //AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(an, // AssemblyBuilderAccess.RunAndSave); //ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(typeSignature + ".MainModule", typeSignature + ".dll", true); var moduleTypes = mModuleBuilder.GetTypes().ToList(); //todo проверить список типо на наличие уже сгенерированного //todo и вообще сделать это на уровень выше и если тип уже есть то идти по стандартному пути получения конструктора //todo а ещё лучше тут не конструктор получать а генерировать и задавать тип для PropertyAttributeInformation if (moduleTypes.FirstOrDefault(x => x.Name == typeSignature) == null) { } TypeBuilder tb = mModuleBuilder.DefineType(typeSignature, TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.AutoClass | TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit | TypeAttributes.AutoLayout, null); tb.SetParent(typeof(Attribute)); tb.DefineDefaultConstructor(MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName); foreach (var val in attr.AttributeValues) { var field = new PropertyInformation { PropertyName = val.Key, PropertyValue = val.Value, PropertyType = val.Value.GetType() }; CreateProperty(field, tb); } Type[] types = new Type[attr.AttributeValues.Count]; int i = 0; foreach (var attrProp in attr.AttributeValues) { types[i] = attrProp.Value.GetType(); i++; } var constructor = tb.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, types); ILGenerator myConstructorIL = constructor.GetILGenerator(); myConstructorIL.Emit(OpCodes.Ldarg_0); myConstructorIL.Emit(OpCodes.Ldarg_1); myConstructorIL.Emit(OpCodes.Stfld, attr.AttributeValues.FirstOrDefault().Key); myConstructorIL.Emit(OpCodes.Ret); return(constructor as ConstructorInfo); }