public PersistentAttribute(string name, IGetterSetter accessor) { Name = name; Accessor = accessor; }
public PersistentAttribute(string name, IGetterSetter accessor, int arrLength) { Accessor = accessor; Length = arrLength; Name = name; }
internal void AddToModule(ModuleBuilder module) { string typeName = (nextTypeId++).ToString(); // // Define a public class named "Property" in the assembly. // TypeBuilder myType = module.DefineType(typeName, TypeAttributes.Public); // // Mark the class as implementing IPropertyAccessor. // myType.AddInterfaceImplementation(typeof(IGetterSetter)); // Add a constructor //ConstructorBuilder constructor = myType.DefineDefaultConstructor(MethodAttributes.Public); // // Define a method for the get operation. // Type[] getParamTypes = new [] {typeof(object)}; Type getReturnType = typeof(object); MethodBuilder getMethod = myType.DefineMethod("Get", MethodAttributes.Public | MethodAttributes.Virtual, getReturnType, getParamTypes); // // From the method, get an ILGenerator. This is used to // emit the IL that we want. // ILGenerator getIL = getMethod.GetILGenerator(); // // Emit the IL. // var targetGetMethod = mProperty.GetGetMethod(); if(targetGetMethod != null) { getIL.DeclareLocal(typeof(object)); getIL.Emit(OpCodes.Ldarg_1); //Load the first argument //(target object) getIL.Emit(OpCodes.Castclass, mTargetType); //Cast to the source type getIL.EmitCall(OpCodes.Call, targetGetMethod, null); //Get the property value if(targetGetMethod.ReturnType.IsValueType) { getIL.Emit(OpCodes.Box, targetGetMethod.ReturnType); //Box if necessary } getIL.Emit(OpCodes.Stloc_0); //Store it getIL.Emit(OpCodes.Ldloc_0); } else { getIL.ThrowException(typeof(MissingMethodException)); } getIL.Emit(OpCodes.Ret); // // Define a method for the set operation. // Type[] setParamTypes = new [] {typeof(object), typeof(object)}; Type setReturnType = null; MethodBuilder setMethod = myType.DefineMethod("Set", MethodAttributes.Public | MethodAttributes.Virtual, setReturnType, setParamTypes); // // From the method, get an ILGenerator. This is used to // emit the IL that we want. // ILGenerator setIL = setMethod.GetILGenerator(); // // Emit the IL. // MethodInfo targetSetMethod = mProperty.GetSetMethod(); if(targetSetMethod != null) { Type paramType = targetSetMethod.GetParameters()[0].ParameterType; setIL.DeclareLocal(paramType); setIL.Emit(OpCodes.Ldarg_1); //Load the first argument //(target object) setIL.Emit(OpCodes.Castclass, mTargetType); //Cast to the source type setIL.Emit(OpCodes.Ldarg_2); //Load the second argument //(value object) if(paramType.IsValueType) { setIL.Emit(OpCodes.Unbox, paramType); //Unbox it if(typesHashes[paramType]!=null) //and load { OpCode load = (OpCode)typesHashes[paramType]; setIL.Emit(load); } else { setIL.Emit(OpCodes.Ldobj,paramType); } } else { setIL.Emit(OpCodes.Castclass, paramType); //Cast class } setIL.EmitCall(OpCodes.Callvirt, targetSetMethod, null); //Set the property value } else { setIL.ThrowException(typeof(MissingMethodException)); } setIL.Emit(OpCodes.Ret); // // Load the type // myType.CreateType(); mEmittedPropertyAccessor = module.Assembly.CreateInstance(typeName) as IGetterSetter; if (mEmittedPropertyAccessor == null) { throw new Exception("Unable to create property accessor."); } }
public PersistentAttribute(IGetterSetter accessor) { Accessor = accessor; }
public NestedSimpleDataField(DataHolderDefinition dataHolder, string name, IGetterSetter accessor, MemberInfo mappedMember, IProducer producer, INestedDataField parent) : base(dataHolder, name, accessor, mappedMember, producer, parent) { }
/// <summary> /// Prescans the type. /// </summary> protected virtual void Scan(SerializerOptions options, bool includeIfUnattributed) { XmlApproachAttribute approach = null; XmlElementAttribute elementAttribute; // Get the applicable attributes LoadAttributes(options); // Get the setter/getter and serializer if (memberInfo is FieldInfo) { getterSetter = new FieldGetterSetter(memberInfo); returnType = ((FieldInfo) memberInfo).FieldType; } else if (memberInfo is PropertyInfo) { var propertyInfo = (PropertyInfo) memberInfo; getterSetter = new PropertyGetterSetter(memberInfo); returnType = ((PropertyInfo) memberInfo).PropertyType; } else if (memberInfo is Type) { getterSetter = null; serializedName = memberInfo.Name; returnType = (Type) memberInfo; } else { throw new ArgumentException($"Unsupported member type: {this.memberInfo.MemberType.ToString()}"); } // Get the [XmlExclude] [XmlAttribute] or [XmlElement] attribute var attribute = GetFirstApplicableAttribute(false, typeof (XmlExcludeAttribute),typeof(XmlTextAttribute), typeof (XmlAttributeAttribute), typeof (XmlElementAttribute)); if (attribute != null) { if (attribute is XmlExcludeAttribute) { // This member needs to be excluded serializedNodeType = XmlNodeType.None; } else if (attribute is XmlTextAttribute) { serializedNodeType = XmlNodeType.Text; } else if ((approach = attribute as XmlApproachAttribute) != null) { ApproachAttribute = approach; // This member needs to be included as an attribute or an element serializedNodeType = approach is XmlElementAttribute ? XmlNodeType.Element : XmlNodeType.Attribute; if (approach.Type != null) { returnType = approach.Type; } serializedName = approach.Name; serializedNamespace = approach.Namespace; if ((elementAttribute = approach as XmlElementAttribute) != null) { if (elementAttribute.SerializeAsValueNode) { serializeAsValueNodeAttributeName = elementAttribute.ValueNodeAttributeName; } } if (approach.SerializeUnattribted) { this.includeIfUnattributed = true; } if (approach.SerializeIfNull) this.serializeIfNull = true; } } else { if (includeIfUnattributed) { serializedName = memberInfo.Name; serializedNodeType = XmlNodeType.Element; } else { serializedNodeType = XmlNodeType.None; } } if (serializedNodeType == XmlNodeType.None) { return; } // Check if the member should be serialized as CDATA attribute = GetFirstApplicableAttribute(typeof (XmlCDataAttribute)); if (attribute != null) { serializeAsCData = ((XmlCDataAttribute) attribute).Enabled; } attribute = GetFirstApplicableAttribute(typeof (XmlVariableSubstitutionAttribute)); if (attribute != null) { Substitutor = (IVariableSubstitutor) Activator.CreateInstance(((XmlVariableSubstitutionAttribute) attribute).SubstitutorType); } // Set the serialized (element or attribute) name to the name of the member if it hasn't already been set if (serializedName.Length == 0) { if (approach != null && approach.UseNameFromAttributedType && memberInfo.MemberType == MemberTypes.TypeInfo) { serializedName = GetAttributeDeclaringType((Type) memberInfo, approach).Name; } else { serializedName = this.memberInfo.Name.Left(PredicateUtils.ObjectEquals('`').Not()); } } // Make the serialized (element or attribute) name lowercase if requested if (approach != null) { if (approach.MakeNameLowercase) { serializedName = serializedName.ToLower(); } } // Get the explicitly specified TypeSerializer if requested attribute = GetFirstApplicableAttribute(typeof (XmlTypeSerializerTypeAttribute)); if (attribute != null) { if (((XmlTypeSerializerTypeAttribute) attribute).SerializerType != null) { typeSerializer = typeSerializerCache.GetTypeSerializerBySerializerType(((XmlTypeSerializerTypeAttribute) attribute).SerializerType, this); if (!returnType.IsAssignableFrom(typeSerializer.SupportedType)) { throw new InvalidOperationException($"Explicitly specified serializer ({((XmlTypeSerializerTypeAttribute)attribute).SerializerType.Name}) doesn't support serializing of associated program element."); } } } else { typeSerializer = typeSerializerCache.GetTypeSerializerBySupportedType(returnType, this); } // Check if the member should be treated as a null value if it is empty treatAsNullIfEmpty = HasApplicableAttribute(typeof (XmlTreatAsNullIfEmptyAttribute)); // Check if the member's declared type is polymorphic var polymorphicTypeAttribute = (XmlPolymorphicTypeAttribute) GetFirstApplicableAttribute(typeof (XmlPolymorphicTypeAttribute)); if (polymorphicTypeAttribute != null) { polymorphicTypeProvider = (IXmlDynamicTypeProvider) Activator.CreateInstance(polymorphicTypeAttribute.PolymorphicTypeProvider); } }
public NestedArrayDataField(DataHolderDefinition dataHolder, string name, IGetterSetter accessor, MemberInfo mappedMember, IProducer producer, IProducer arrayProducer, int length, INestedDataField parent) : base(dataHolder, name, accessor, mappedMember, parent, length, arrayProducer) { m_Producer = producer; m_ArrayAccessors = new NestedArrayAccessor[m_length]; for (var i = 0; i < m_length; i++) { m_ArrayAccessors[i] = new NestedArrayAccessor(this, i); } }
protected NestedDataField(DataHolderDefinition dataHolder, string name, IGetterSetter accessor, MemberInfo mappedMember, IProducer producer, INestedDataField parent) : base(dataHolder, name, accessor, mappedMember, parent) { m_Producer = producer; }
protected DataField(DataHolderDefinition dataHolder, string name, IGetterSetter accessor, MemberInfo mappedMember, INestedDataField parent) : base(dataHolder, accessor, parent) { m_mappedMember = mappedMember; m_name = name; //m_fullName = parent; }
protected DataFieldBase(DataHolderDefinition dataHolder, IGetterSetter accessor, INestedDataField parent) { m_DataHolderDefinition = dataHolder; m_parent = parent; m_accessor = accessor; }
protected ArrayDataField(DataHolderDefinition dataHolder, string name, IGetterSetter accessor, MemberInfo mappedMember, INestedDataField parent, int length, IProducer arrProducer) : base(dataHolder, name, accessor, mappedMember, parent) { m_length = length; m_arrProducer = arrProducer; }
/// <summary> /// Prescans the type. /// </summary> protected virtual void Scan(SerializerOptions options, bool includeIfUnattributed) { XmlApproachAttribute approach = null; XmlElementAttribute elementAttribute; // Get the applicable attributes LoadAttributes(options); // Get the setter/getter and serializer if (memberInfo is FieldInfo) { getterSetter = new FieldGetterSetter(memberInfo); returnType = ((FieldInfo)memberInfo).FieldType; } else if (memberInfo is PropertyInfo) { var propertyInfo = (PropertyInfo)memberInfo; getterSetter = new PropertyGetterSetter(memberInfo); returnType = ((PropertyInfo)memberInfo).PropertyType; } else if (memberInfo is Type) { getterSetter = null; serializedName = memberInfo.Name; returnType = (Type)memberInfo; } else { throw new ArgumentException(String.Format("Unsupported member type: {0}", memberInfo.MemberType.ToString())); } // Get the [XmlExclude] [XmlAttribute] or [XmlElement] attribute var attribute = GetFirstApplicableAttribute(false, typeof(XmlExcludeAttribute), typeof(XmlTextAttribute), typeof(XmlAttributeAttribute), typeof(XmlElementAttribute)); if (attribute != null) { if (attribute is XmlExcludeAttribute) { // This member needs to be excluded serializedNodeType = XmlNodeType.None; } else if (attribute is XmlTextAttribute) { serializedNodeType = XmlNodeType.Text; } else if ((approach = attribute as XmlApproachAttribute) != null) { ApproachAttribute = approach; // This member needs to be included as an attribute or an element serializedNodeType = approach is XmlElementAttribute ? XmlNodeType.Element : XmlNodeType.Attribute; if (approach.Type != null) { returnType = approach.Type; } serializedName = approach.Name; serializedNamespace = approach.Namespace; if ((elementAttribute = approach as XmlElementAttribute) != null) { if (elementAttribute.SerializeAsValueNode) { serializeAsValueNodeAttributeName = elementAttribute.ValueNodeAttributeName; } } if (approach.SerializeUnattribted) { this.includeIfUnattributed = true; } if (approach.SerializeIfNull) { this.serializeIfNull = true; } } } else { if (includeIfUnattributed) { serializedName = memberInfo.Name; serializedNodeType = XmlNodeType.Element; } else { serializedNodeType = XmlNodeType.None; } } if (serializedNodeType == XmlNodeType.None) { return; } // Check if the member should be serialized as CDATA attribute = GetFirstApplicableAttribute(typeof(XmlCDataAttribute)); if (attribute != null) { serializeAsCData = ((XmlCDataAttribute)attribute).Enabled; } attribute = GetFirstApplicableAttribute(typeof(XmlVariableSubstitutionAttribute)); if (attribute != null) { Substitutor = (IVariableSubstitutor)Activator.CreateInstance(((XmlVariableSubstitutionAttribute)attribute).SubstitutorType); } // Set the serialized (element or attribute) name to the name of the member if it hasn't already been set if (serializedName.Length == 0) { if (approach != null && approach.UseNameFromAttributedType && memberInfo.MemberType == MemberTypes.TypeInfo) { serializedName = GetAttributeDeclaringType((Type)memberInfo, approach).Name; } else { serializedName = this.memberInfo.Name.Left(PredicateUtils.ObjectEquals('`').Not()); } } // Make the serialized (element or attribute) name lowercase if requested if (approach != null) { if (approach.MakeNameLowercase) { serializedName = serializedName.ToLower(); } } // Get the explicitly specified TypeSerializer if requested attribute = GetFirstApplicableAttribute(typeof(XmlTypeSerializerTypeAttribute)); if (attribute != null) { if (((XmlTypeSerializerTypeAttribute)attribute).SerializerType != null) { typeSerializer = typeSerializerCache.GetTypeSerializerBySerializerType(((XmlTypeSerializerTypeAttribute)attribute).SerializerType, this); if (!returnType.IsAssignableFrom(typeSerializer.SupportedType)) { throw new InvalidOperationException(String.Format("Explicitly specified serializer ({0}) doesn't support serializing of associated program element.", ((XmlTypeSerializerTypeAttribute)attribute).SerializerType.Name)); } } } else { typeSerializer = typeSerializerCache.GetTypeSerializerBySupportedType(returnType, this); } // Check if the member should be treated as a null value if it is empty treatAsNullIfEmpty = HasApplicableAttribute(typeof(XmlTreatAsNullIfEmptyAttribute)); // Check if the member's declared type is polymorphic var polymorphicTypeAttribute = (XmlPolymorphicTypeAttribute)GetFirstApplicableAttribute(typeof(XmlPolymorphicTypeAttribute)); if (polymorphicTypeAttribute != null) { polymorphicTypeProvider = (IXmlDynamicTypeProvider)Activator.CreateInstance(polymorphicTypeAttribute.PolymorphicTypeProvider); } }
public FlatSimpleDataField(DataHolderDefinition dataHolder, string name, IGetterSetter accessor, MemberInfo mappedMember, INestedDataField parent) : base(dataHolder, name, accessor, mappedMember, parent) { }
internal void AddToModule(ModuleBuilder module) { string typeName = (nextTypeId++).ToString(); // // Define a public class named "Property" in the assembly. // TypeBuilder myType = module.DefineType(typeName, TypeAttributes.Public); // // Mark the class as implementing IPropertyAccessor. // myType.AddInterfaceImplementation(typeof(IGetterSetter)); // Add a constructor //ConstructorBuilder constructor = myType.DefineDefaultConstructor(MethodAttributes.Public); // // Define a method for the get operation. // Type[] getParamTypes = new [] { typeof(object) }; Type getReturnType = typeof(object); MethodBuilder getMethod = myType.DefineMethod("Get", MethodAttributes.Public | MethodAttributes.Virtual, getReturnType, getParamTypes); // // From the method, get an ILGenerator. This is used to // emit the IL that we want. // ILGenerator getIL = getMethod.GetILGenerator(); // // Emit the IL. // var targetGetMethod = mProperty.GetGetMethod(); if (targetGetMethod != null) { getIL.DeclareLocal(typeof(object)); getIL.Emit(OpCodes.Ldarg_1); //Load the first argument //(target object) getIL.Emit(OpCodes.Castclass, mTargetType); //Cast to the source type getIL.EmitCall(OpCodes.Call, targetGetMethod, null); //Get the property value if (targetGetMethod.ReturnType.IsValueType) { getIL.Emit(OpCodes.Box, targetGetMethod.ReturnType); //Box if necessary } getIL.Emit(OpCodes.Stloc_0); //Store it getIL.Emit(OpCodes.Ldloc_0); } else { getIL.ThrowException(typeof(MissingMethodException)); } getIL.Emit(OpCodes.Ret); // // Define a method for the set operation. // Type[] setParamTypes = new [] { typeof(object), typeof(object) }; Type setReturnType = null; MethodBuilder setMethod = myType.DefineMethod("Set", MethodAttributes.Public | MethodAttributes.Virtual, setReturnType, setParamTypes); // // From the method, get an ILGenerator. This is used to // emit the IL that we want. // ILGenerator setIL = setMethod.GetILGenerator(); // // Emit the IL. // MethodInfo targetSetMethod = mProperty.GetSetMethod(); if (targetSetMethod != null) { Type paramType = targetSetMethod.GetParameters()[0].ParameterType; setIL.DeclareLocal(paramType); setIL.Emit(OpCodes.Ldarg_1); //Load the first argument //(target object) setIL.Emit(OpCodes.Castclass, mTargetType); //Cast to the source type setIL.Emit(OpCodes.Ldarg_2); //Load the second argument //(value object) if (paramType.IsValueType) { setIL.Emit(OpCodes.Unbox, paramType); //Unbox it if (typesHashes[paramType] != null) //and load { OpCode load = (OpCode)typesHashes[paramType]; setIL.Emit(load); } else { setIL.Emit(OpCodes.Ldobj, paramType); } } else { setIL.Emit(OpCodes.Castclass, paramType); //Cast class } setIL.EmitCall(OpCodes.Callvirt, targetSetMethod, null); //Set the property value } else { setIL.ThrowException(typeof(MissingMethodException)); } setIL.Emit(OpCodes.Ret); // // Load the type // myType.CreateType(); mEmittedPropertyAccessor = module.Assembly.CreateInstance(typeName) as IGetterSetter; if (mEmittedPropertyAccessor == null) { throw new Exception("Unable to create property accessor."); } }
public static IGetterSetter AddToModule(ModuleBuilder module, PropertyInfo prop) { string str = NextTypeName(prop.GetFullMemberName()); Type declaringType = prop.DeclaringType; TypeBuilder typeBuilder = module.DefineType(str, TypeAttributes.Public); typeBuilder.AddInterfaceImplementation(typeof(IGetterSetter)); Type[] parameterTypes1 = new Type[1] { typeof(object) }; Type returnType1 = typeof(object); ILGenerator ilGenerator1 = typeBuilder.DefineMethod("Get", MethodAttributes.Public | MethodAttributes.Virtual, returnType1, parameterTypes1).GetILGenerator(); MethodInfo getMethod = prop.GetGetMethod(); if (getMethod != null) { ilGenerator1.DeclareLocal(typeof(object)); ilGenerator1.Emit(OpCodes.Ldarg_1); ilGenerator1.Emit(OpCodes.Castclass, declaringType); ilGenerator1.EmitCall(OpCodes.Call, getMethod, null); if (getMethod.ReturnType.IsValueType) { ilGenerator1.Emit(OpCodes.Box, getMethod.ReturnType); } ilGenerator1.Emit(OpCodes.Stloc_0); ilGenerator1.Emit(OpCodes.Ldloc_0); } else { ilGenerator1.ThrowException(typeof(MissingMethodException)); } ilGenerator1.Emit(OpCodes.Ret); Type[] parameterTypes2 = new Type[2] { typeof(object), typeof(object) }; Type returnType2 = null; ILGenerator ilGenerator2 = typeBuilder.DefineMethod("Set", MethodAttributes.Public | MethodAttributes.Virtual, returnType2, parameterTypes2).GetILGenerator(); MethodInfo setMethod = prop.GetSetMethod(); if (setMethod != null) { Type parameterType = setMethod.GetParameters()[0].ParameterType; ilGenerator2.DeclareLocal(parameterType); ilGenerator2.Emit(OpCodes.Ldarg_1); ilGenerator2.Emit(OpCodes.Castclass, declaringType); ilGenerator2.Emit(OpCodes.Ldarg_2); if (parameterType.IsValueType) { ilGenerator2.Emit(OpCodes.Unbox, parameterType); OpCode opcode; if (PropTypesHashes.TryGetValue(parameterType, out opcode)) { ilGenerator2.Emit(opcode); } else { ilGenerator2.Emit(OpCodes.Ldobj, parameterType); } } else { ilGenerator2.Emit(OpCodes.Castclass, parameterType); } ilGenerator2.EmitCall(OpCodes.Callvirt, setMethod, null); } else { ilGenerator2.ThrowException(typeof(MissingMethodException)); } ilGenerator2.Emit(OpCodes.Ret); typeBuilder.CreateType(); IGetterSetter instance = module.Assembly.CreateInstance(str) as IGetterSetter; if (instance == null) { throw new Exception("Unable to create property accessor for \"" + prop + "\" of type: " + prop.DeclaringType.FullName); } return(instance); }