예제 #1
0
        private void EmitGetHashCode(PropertyAndField[] properties, MethodBuilder method)
        {
            MethodInfo  methodInfo  = ObjectType.GetMethod("GetHashCode", Type.EmptyTypes);
            ILGenerator iLGenerator = method.GetILGenerator();

            iLGenerator.Emit(OpCodes.Ldc_I4, method.GetHashCode());
            int index = 0;

            if (0 < properties.Length)
            {
                do
                {
                    PropertyAndField field   = properties[index];
                    FieldBuilder     builder = field.Field;
                    if (!field.Property.CanWrite)
                    {
                        iLGenerator.Emit(OpCodes.Ldc_I4, 0x1f);
                        iLGenerator.Emit(OpCodes.Mul);
                        Label label = iLGenerator.DefineLabel();
                        iLGenerator.Emit(OpCodes.Ldarg_0);
                        iLGenerator.Emit(OpCodes.Ldfld, builder);
                        iLGenerator.Emit(OpCodes.Box, builder.FieldType);
                        iLGenerator.Emit(OpCodes.Brfalse_S, label);
                        iLGenerator.Emit(OpCodes.Ldarg_0);
                        iLGenerator.Emit(OpCodes.Ldflda, builder);
                        iLGenerator.Emit(OpCodes.Constrained, builder.FieldType);
                        iLGenerator.EmitCall(OpCodes.Callvirt, methodInfo, null);
                        iLGenerator.Emit(OpCodes.Add);
                        iLGenerator.MarkLabel(label);
                    }
                    index++;
                }while (index < properties.Length);
            }
            iLGenerator.Emit(OpCodes.Ret);
        }
예제 #2
0
        public override IEnumerable <PropertyAndField <TModel> > GetPropertyInfoes
            (System.Reflection.BindingFlags bindFlags, PropertyFilterDelegate2 filter)
        {
            Type ModelType = typeof(TModel);
            IEnumerable <PropertyInfo>        properties = _properties.Select(e => e.Key);
            List <PropertyAndField <TModel> > list       = new List <PropertyAndField <TModel> >();

            foreach (var p in properties)
            {
                if (filter != null)
                {
                    if (!filter(p))
                    {
                        continue;
                    }
                }
                PropertyAndField <TModel> pf = new PropertyAndField <TModel>();
                pf.DatabaseId   = GetDatabaseIdFromPropertyInfo(p);
                pf.DatabaseName = GetDatabaseNameFromPropertyInfo(p);
                pf.DatabaseType = GetDataTypeFromPropertyInfo(p);
                pf.AccessLevel  = GetAccessLevelFromPropertyInfo(p);
                pf.Property     = p;
                pf.Getter       = _properties[p].Getter;
                pf.Setter       = _properties[p].Setter;
                list.Add(pf);
            }

            return(list);
        }
예제 #3
0
        public unsafe Type EmitType(BCSYM_NamedRoot *pSymbol)
        {
            TypeBuilder builder = this.DefineType(BCSYM.PNamedRoot((BCSYM modopt(IsConst) * modopt(IsConst) modopt(IsConst))pSymbol));

            this.m_types.Add(builder.Name, builder);
            bool hasKey = false;
            int  num2   = BCSYM.GetGenericParamCount((BCSYM * modopt(IsConst) modopt(IsConst))pSymbol);

            PropertyAndField[]  properties = new PropertyAndField[num2];
            BCSYM_GenericParam *pParam     = BCSYM.GetFirstGenericParam((BCSYM * modopt(IsConst) modopt(IsConst))pSymbol);
            int index = 0;

            if (0 < num2)
            {
                do
                {
                    PropertyAndField prop = this.FindPropertyAndField(pSymbol, pParam, builder, &hasKey);
                    properties[index] = prop;
                    this.EmitProperty(prop);
                    pParam = *((BCSYM_GenericParam **)(pParam + 80));
                    index++;
                }while (index < num2);
            }
            ConstructorBuilder ctorBuilder       = null;
            MethodBuilder      toStringBuilder   = null;
            MethodBuilder      equalsObjBuilder  = null;
            MethodBuilder      equalsTypeBuilder = null;
            MethodBuilder      getHashBuilder    = null;

            this.FindMethods(pSymbol, builder, ref ctorBuilder, ref toStringBuilder, ref equalsObjBuilder, ref equalsTypeBuilder, ref getHashBuilder);
            this.EmitCtor(properties, ctorBuilder);
            this.EmitToString(properties, toStringBuilder);
            if (hasKey)
            {
                Type interfaceType = typeof(IEquatable <>).MakeGenericType(new Type[] { builder });
                builder.AddInterfaceImplementation(interfaceType);
                this.EmitEqualsTyped(properties, equalsTypeBuilder);
                this.EmitEqualsObj(builder, equalsObjBuilder, equalsTypeBuilder);
                this.EmitGetHashCode(properties, getHashBuilder);
            }
            Type type = builder.CreateType();

            this.m_types[builder.Name] = type;
            return(type);
        }
예제 #4
0
        private void EmitToString(PropertyAndField[] properties, MethodBuilder method)
        {
            ILGenerator iLGenerator = method.GetILGenerator();

            Type[]     types      = new Type[] { StringType };
            MethodInfo methodInfo = StringBuilderType.GetMethod("Append", types);

            Type[]     typeArray = new Type[] { StringType, ObjectType, ObjectType };
            MethodInfo info      = StringBuilderType.GetMethod("AppendFormat", typeArray);

            iLGenerator.Emit(OpCodes.Newobj, StringBuilderType.GetConstructor(Type.EmptyTypes));
            iLGenerator.Emit(OpCodes.Ldstr, "{ ");
            iLGenerator.EmitCall(OpCodes.Callvirt, methodInfo, null);
            int index = 0;

            if (0 < (properties.Length - 1))
            {
                do
                {
                    PropertyAndField field    = properties[index];
                    PropertyAndField field3   = field;
                    FieldBuilder     builder2 = field.Field;
                    iLGenerator.Emit(OpCodes.Ldstr, "{0} = {1}, ");
                    iLGenerator.Emit(OpCodes.Ldstr, field3.Property.Name);
                    iLGenerator.Emit(OpCodes.Ldarg_0);
                    iLGenerator.Emit(OpCodes.Ldfld, builder2);
                    iLGenerator.Emit(OpCodes.Box, builder2.FieldType);
                    iLGenerator.EmitCall(OpCodes.Callvirt, info, null);
                    index++;
                }while (index < (properties.Length - 1));
            }
            PropertyAndField field2  = properties[properties.Length - 1];
            FieldBuilder     builder = properties[index].Field;

            iLGenerator.Emit(OpCodes.Ldstr, "{0} = {1} ");
            iLGenerator.Emit(OpCodes.Ldstr, field2.Property.Name);
            iLGenerator.Emit(OpCodes.Ldarg_0);
            iLGenerator.Emit(OpCodes.Ldfld, builder);
            iLGenerator.Emit(OpCodes.Box, builder.FieldType);
            iLGenerator.EmitCall(OpCodes.Callvirt, info, null);
            iLGenerator.Emit(OpCodes.Ldstr, "}");
            iLGenerator.EmitCall(OpCodes.Callvirt, methodInfo, null);
            iLGenerator.EmitCall(OpCodes.Callvirt, StringBuilderType.GetMethod("ToString", Type.EmptyTypes), null);
            iLGenerator.Emit(OpCodes.Ret);
        }
예제 #5
0
        private void EmitProperty(PropertyAndField prop)
        {
            ILGenerator iLGenerator = ((MethodBuilder)prop.Property.GetGetMethod(false)).GetILGenerator();

            iLGenerator.Emit(OpCodes.Ldarg_0);
            iLGenerator.Emit(OpCodes.Ldfld, prop.Field);
            iLGenerator.Emit(OpCodes.Ret);
            MethodBuilder setMethod = (MethodBuilder)prop.Property.GetSetMethod(false);

            if (setMethod != null)
            {
                iLGenerator = setMethod.GetILGenerator();
                iLGenerator.Emit(OpCodes.Ldarg_0);
                iLGenerator.Emit(OpCodes.Ldarg_1);
                iLGenerator.Emit(OpCodes.Stfld, prop.Field);
                iLGenerator.Emit(OpCodes.Ret);
            }
        }
 private void EmitCtor(PropertyAndField[] properties, ConstructorBuilder method)
 {
     ILGenerator iLGenerator = method.GetILGenerator();
     iLGenerator.Emit(OpCodes.Ldarg_0);
     iLGenerator.Emit(OpCodes.Call, ObjectType.GetConstructor(Type.EmptyTypes));
     int index = 0;
     if (0 < properties.Length)
     {
         do
         {
             iLGenerator.Emit(OpCodes.Ldarg_0);
             int arg = index + 1;
             iLGenerator.Emit(OpCodes.Ldarg, arg);
             iLGenerator.Emit(OpCodes.Stfld, properties[index].Field);
             index = arg;
         }
         while (index < properties.Length);
     }
     iLGenerator.Emit(OpCodes.Ret);
 }
예제 #7
0
        private void EmitEqualsTyped(PropertyAndField[] properties, MethodBuilder method)
        {
            MethodInfo  methodInfo  = ObjectType.GetMethod("Equals", BindingFlags.Public | BindingFlags.Instance);
            ILGenerator iLGenerator = method.GetILGenerator();
            Label       label4      = iLGenerator.DefineLabel();

            iLGenerator.Emit(OpCodes.Ldarg_1);
            iLGenerator.Emit(OpCodes.Brtrue_S, label4);
            iLGenerator.Emit(OpCodes.Ldc_I4_0);
            iLGenerator.Emit(OpCodes.Ret);
            iLGenerator.MarkLabel(label4);
            int index = 0;

            if (0 < properties.Length)
            {
                do
                {
                    PropertyAndField field = properties[index];
                    FieldInfo        info  = field.Field;
                    if (!field.Property.CanWrite)
                    {
                        Label label3 = iLGenerator.DefineLabel();
                        iLGenerator.Emit(OpCodes.Ldarg_0);
                        iLGenerator.Emit(OpCodes.Ldfld, info);
                        iLGenerator.Emit(OpCodes.Box, info.FieldType);
                        iLGenerator.Emit(OpCodes.Brtrue_S, label3);
                        Label label2 = iLGenerator.DefineLabel();
                        iLGenerator.Emit(OpCodes.Ldarg_1);
                        iLGenerator.Emit(OpCodes.Ldfld, info);
                        iLGenerator.Emit(OpCodes.Box, info.FieldType);
                        iLGenerator.Emit(OpCodes.Brfalse_S, label2);
                        iLGenerator.Emit(OpCodes.Ldc_I4_0);
                        iLGenerator.Emit(OpCodes.Ret);
                        iLGenerator.MarkLabel(label3);
                        iLGenerator.Emit(OpCodes.Ldarg_1);
                        iLGenerator.Emit(OpCodes.Ldfld, info);
                        iLGenerator.Emit(OpCodes.Box, info.FieldType);
                        iLGenerator.Emit(OpCodes.Brtrue_S, label2);
                        iLGenerator.Emit(OpCodes.Ldc_I4_0);
                        iLGenerator.Emit(OpCodes.Ret);
                        iLGenerator.MarkLabel(label2);
                        Label label = iLGenerator.DefineLabel();
                        iLGenerator.Emit(OpCodes.Ldarg_0);
                        iLGenerator.Emit(OpCodes.Ldfld, info);
                        iLGenerator.Emit(OpCodes.Box, info.FieldType);
                        iLGenerator.Emit(OpCodes.Brfalse_S, label);
                        iLGenerator.Emit(OpCodes.Ldarg_1);
                        iLGenerator.Emit(OpCodes.Ldfld, info);
                        iLGenerator.Emit(OpCodes.Box, info.FieldType);
                        iLGenerator.Emit(OpCodes.Brfalse_S, label);
                        iLGenerator.Emit(OpCodes.Ldarg_0);
                        iLGenerator.Emit(OpCodes.Ldfld, info);
                        iLGenerator.Emit(OpCodes.Box, info.FieldType);
                        iLGenerator.Emit(OpCodes.Ldarg_1);
                        iLGenerator.Emit(OpCodes.Ldfld, info);
                        iLGenerator.Emit(OpCodes.Box, info.FieldType);
                        iLGenerator.EmitCall(OpCodes.Callvirt, methodInfo, null);
                        iLGenerator.Emit(OpCodes.Brtrue_S, label);
                        iLGenerator.Emit(OpCodes.Ldc_I4_0);
                        iLGenerator.Emit(OpCodes.Ret);
                        iLGenerator.MarkLabel(label);
                    }
                    index++;
                }while (index < properties.Length);
            }
            iLGenerator.Emit(OpCodes.Ldc_I4_1);
            iLGenerator.Emit(OpCodes.Ret);
        }
 private void EmitEqualsTyped(PropertyAndField[] properties, MethodBuilder method)
 {
     MethodInfo methodInfo = ObjectType.GetMethod("Equals", BindingFlags.Public | BindingFlags.Instance);
     ILGenerator iLGenerator = method.GetILGenerator();
     Label label4 = iLGenerator.DefineLabel();
     iLGenerator.Emit(OpCodes.Ldarg_1);
     iLGenerator.Emit(OpCodes.Brtrue_S, label4);
     iLGenerator.Emit(OpCodes.Ldc_I4_0);
     iLGenerator.Emit(OpCodes.Ret);
     iLGenerator.MarkLabel(label4);
     int index = 0;
     if (0 < properties.Length)
     {
         do
         {
             PropertyAndField field = properties[index];
             FieldInfo info = field.Field;
             if (!field.Property.CanWrite)
             {
                 Label label3 = iLGenerator.DefineLabel();
                 iLGenerator.Emit(OpCodes.Ldarg_0);
                 iLGenerator.Emit(OpCodes.Ldfld, info);
                 iLGenerator.Emit(OpCodes.Box, info.FieldType);
                 iLGenerator.Emit(OpCodes.Brtrue_S, label3);
                 Label label2 = iLGenerator.DefineLabel();
                 iLGenerator.Emit(OpCodes.Ldarg_1);
                 iLGenerator.Emit(OpCodes.Ldfld, info);
                 iLGenerator.Emit(OpCodes.Box, info.FieldType);
                 iLGenerator.Emit(OpCodes.Brfalse_S, label2);
                 iLGenerator.Emit(OpCodes.Ldc_I4_0);
                 iLGenerator.Emit(OpCodes.Ret);
                 iLGenerator.MarkLabel(label3);
                 iLGenerator.Emit(OpCodes.Ldarg_1);
                 iLGenerator.Emit(OpCodes.Ldfld, info);
                 iLGenerator.Emit(OpCodes.Box, info.FieldType);
                 iLGenerator.Emit(OpCodes.Brtrue_S, label2);
                 iLGenerator.Emit(OpCodes.Ldc_I4_0);
                 iLGenerator.Emit(OpCodes.Ret);
                 iLGenerator.MarkLabel(label2);
                 Label label = iLGenerator.DefineLabel();
                 iLGenerator.Emit(OpCodes.Ldarg_0);
                 iLGenerator.Emit(OpCodes.Ldfld, info);
                 iLGenerator.Emit(OpCodes.Box, info.FieldType);
                 iLGenerator.Emit(OpCodes.Brfalse_S, label);
                 iLGenerator.Emit(OpCodes.Ldarg_1);
                 iLGenerator.Emit(OpCodes.Ldfld, info);
                 iLGenerator.Emit(OpCodes.Box, info.FieldType);
                 iLGenerator.Emit(OpCodes.Brfalse_S, label);
                 iLGenerator.Emit(OpCodes.Ldarg_0);
                 iLGenerator.Emit(OpCodes.Ldfld, info);
                 iLGenerator.Emit(OpCodes.Box, info.FieldType);
                 iLGenerator.Emit(OpCodes.Ldarg_1);
                 iLGenerator.Emit(OpCodes.Ldfld, info);
                 iLGenerator.Emit(OpCodes.Box, info.FieldType);
                 iLGenerator.EmitCall(OpCodes.Callvirt, methodInfo, null);
                 iLGenerator.Emit(OpCodes.Brtrue_S, label);
                 iLGenerator.Emit(OpCodes.Ldc_I4_0);
                 iLGenerator.Emit(OpCodes.Ret);
                 iLGenerator.MarkLabel(label);
             }
             index++;
         }
         while (index < properties.Length);
     }
     iLGenerator.Emit(OpCodes.Ldc_I4_1);
     iLGenerator.Emit(OpCodes.Ret);
 }
 public unsafe Type EmitType(BCSYM_NamedRoot* pSymbol)
 {
     TypeBuilder builder = this.DefineType(BCSYM.PNamedRoot((BCSYM modopt(IsConst)* modopt(IsConst) modopt(IsConst)) pSymbol));
     this.m_types.Add(builder.Name, builder);
     bool hasKey = false;
     int num2 = BCSYM.GetGenericParamCount((BCSYM* modopt(IsConst) modopt(IsConst)) pSymbol);
     PropertyAndField[] properties = new PropertyAndField[num2];
     BCSYM_GenericParam* pParam = BCSYM.GetFirstGenericParam((BCSYM* modopt(IsConst) modopt(IsConst)) pSymbol);
     int index = 0;
     if (0 < num2)
     {
         do
         {
             PropertyAndField prop = this.FindPropertyAndField(pSymbol, pParam, builder, &hasKey);
             properties[index] = prop;
             this.EmitProperty(prop);
             pParam = *((BCSYM_GenericParam**) (pParam + 80));
             index++;
         }
         while (index < num2);
     }
     ConstructorBuilder ctorBuilder = null;
     MethodBuilder toStringBuilder = null;
     MethodBuilder equalsObjBuilder = null;
     MethodBuilder equalsTypeBuilder = null;
     MethodBuilder getHashBuilder = null;
     this.FindMethods(pSymbol, builder, ref ctorBuilder, ref toStringBuilder, ref equalsObjBuilder, ref equalsTypeBuilder, ref getHashBuilder);
     this.EmitCtor(properties, ctorBuilder);
     this.EmitToString(properties, toStringBuilder);
     if (hasKey)
     {
         Type interfaceType = typeof(IEquatable<>).MakeGenericType(new Type[] { builder });
         builder.AddInterfaceImplementation(interfaceType);
         this.EmitEqualsTyped(properties, equalsTypeBuilder);
         this.EmitEqualsObj(builder, equalsObjBuilder, equalsTypeBuilder);
         this.EmitGetHashCode(properties, getHashBuilder);
     }
     Type type = builder.CreateType();
     this.m_types[builder.Name] = type;
     return type;
 }
 private void EmitProperty(PropertyAndField prop)
 {
     ILGenerator iLGenerator = ((MethodBuilder) prop.Property.GetGetMethod(false)).GetILGenerator();
     iLGenerator.Emit(OpCodes.Ldarg_0);
     iLGenerator.Emit(OpCodes.Ldfld, prop.Field);
     iLGenerator.Emit(OpCodes.Ret);
     MethodBuilder setMethod = (MethodBuilder) prop.Property.GetSetMethod(false);
     if (setMethod != null)
     {
         iLGenerator = setMethod.GetILGenerator();
         iLGenerator.Emit(OpCodes.Ldarg_0);
         iLGenerator.Emit(OpCodes.Ldarg_1);
         iLGenerator.Emit(OpCodes.Stfld, prop.Field);
         iLGenerator.Emit(OpCodes.Ret);
     }
 }
 private void EmitToString(PropertyAndField[] properties, MethodBuilder method)
 {
     ILGenerator iLGenerator = method.GetILGenerator();
     Type[] types = new Type[] { StringType };
     MethodInfo methodInfo = StringBuilderType.GetMethod("Append", types);
     Type[] typeArray = new Type[] { StringType, ObjectType, ObjectType };
     MethodInfo info = StringBuilderType.GetMethod("AppendFormat", typeArray);
     iLGenerator.Emit(OpCodes.Newobj, StringBuilderType.GetConstructor(Type.EmptyTypes));
     iLGenerator.Emit(OpCodes.Ldstr, "{ ");
     iLGenerator.EmitCall(OpCodes.Callvirt, methodInfo, null);
     int index = 0;
     if (0 < (properties.Length - 1))
     {
         do
         {
             PropertyAndField field = properties[index];
             PropertyAndField field3 = field;
             FieldBuilder builder2 = field.Field;
             iLGenerator.Emit(OpCodes.Ldstr, "{0} = {1}, ");
             iLGenerator.Emit(OpCodes.Ldstr, field3.Property.Name);
             iLGenerator.Emit(OpCodes.Ldarg_0);
             iLGenerator.Emit(OpCodes.Ldfld, builder2);
             iLGenerator.Emit(OpCodes.Box, builder2.FieldType);
             iLGenerator.EmitCall(OpCodes.Callvirt, info, null);
             index++;
         }
         while (index < (properties.Length - 1));
     }
     PropertyAndField field2 = properties[properties.Length - 1];
     FieldBuilder builder = properties[index].Field;
     iLGenerator.Emit(OpCodes.Ldstr, "{0} = {1} ");
     iLGenerator.Emit(OpCodes.Ldstr, field2.Property.Name);
     iLGenerator.Emit(OpCodes.Ldarg_0);
     iLGenerator.Emit(OpCodes.Ldfld, builder);
     iLGenerator.Emit(OpCodes.Box, builder.FieldType);
     iLGenerator.EmitCall(OpCodes.Callvirt, info, null);
     iLGenerator.Emit(OpCodes.Ldstr, "}");
     iLGenerator.EmitCall(OpCodes.Callvirt, methodInfo, null);
     iLGenerator.EmitCall(OpCodes.Callvirt, StringBuilderType.GetMethod("ToString", Type.EmptyTypes), null);
     iLGenerator.Emit(OpCodes.Ret);
 }
 private void EmitGetHashCode(PropertyAndField[] properties, MethodBuilder method)
 {
     MethodInfo methodInfo = ObjectType.GetMethod("GetHashCode", Type.EmptyTypes);
     ILGenerator iLGenerator = method.GetILGenerator();
     iLGenerator.Emit(OpCodes.Ldc_I4, method.GetHashCode());
     int index = 0;
     if (0 < properties.Length)
     {
         do
         {
             PropertyAndField field = properties[index];
             FieldBuilder builder = field.Field;
             if (!field.Property.CanWrite)
             {
                 iLGenerator.Emit(OpCodes.Ldc_I4, 0x1f);
                 iLGenerator.Emit(OpCodes.Mul);
                 Label label = iLGenerator.DefineLabel();
                 iLGenerator.Emit(OpCodes.Ldarg_0);
                 iLGenerator.Emit(OpCodes.Ldfld, builder);
                 iLGenerator.Emit(OpCodes.Box, builder.FieldType);
                 iLGenerator.Emit(OpCodes.Brfalse_S, label);
                 iLGenerator.Emit(OpCodes.Ldarg_0);
                 iLGenerator.Emit(OpCodes.Ldflda, builder);
                 iLGenerator.Emit(OpCodes.Constrained, builder.FieldType);
                 iLGenerator.EmitCall(OpCodes.Callvirt, methodInfo, null);
                 iLGenerator.Emit(OpCodes.Add);
                 iLGenerator.MarkLabel(label);
             }
             index++;
         }
         while (index < properties.Length);
     }
     iLGenerator.Emit(OpCodes.Ret);
 }
 public void ShouldConsiderConformingPropertyAndDifferingFieldObjectsNotEqual()
 {
     var first = new PropertyAndField(1, 0);
     var second = new PropertyAndField(1, 1);
     Assert.That(first.Equals(second), Is.False);
 }
 public void ShouldConsiderComformingPropertyAndFieldObjectsEqual()
 {
     var first = new PropertyAndField(1, 1);
     var second = new PropertyAndField(1, 1);
     Assert.That(first.Equals(second), Is.True);
 }
        public void ShouldGenerateStringForPropertyAndFieldObject()
        {
            var value = new PropertyAndField(10, 20);

            var text = value.ToString();
            Assert.That(text, Is.StringMatching("^.*, .*$"));
            Assert.That(text, Is.StringContaining("Value: 10"));
            Assert.That(text, Is.StringContaining("value: 20"));
        }
 public void ShouldGenerateHashCodeForPropertyAndFieldObject()
 {
     var value = new PropertyAndField(10, 20);
     Assert.That(value.GetHashCode(), Is.EqualTo(HashCode.From(10, 20)));
 }