属性IsDefined - 算术溢出异常

时间:2018-01-19 21:35:23

标签: c#

因此,我循环遍历类的所有属性,以查看是否存在具有属性X

的任何属性

所以这是我的代码:

PropertyInfo[] properties = GetType().GetProperties().Where(x => Attribute.IsDefined(x, typeof(Column))).ToArray();

奇怪的是,这会导致以下错误:Arithmetic operation resulted in an overflow.

如此简单的C#函数如何导致溢出?

我已尝试在即时窗口中检查一些事情,但这并不会产生任何奇怪的结果:

{Secret.Datasets.Wiki.Item Item}
    Attributes: None
    CanRead: false
    CanWrite: true
    CustomAttributes: Count = 0
    DeclaringType: {Name = "Tag" FullName = "Tag"}
    GetMethod: null
    IsSpecialName: false
    MemberType: Property
    MetadataToken: 385875972
    Module: {DynamicRestAssembly}
    Name: "Item"
    PropertyType: {Name = "Item" FullName = "Secret.Datasets.Wiki.Item"}
    ReflectedType: {Name = "Tag" FullName = "Tag"}
    SetMethod: {Secret.Datasets.Wiki.Item get_Item()}

属性:

{Name = "Column" FullName = "Rest.Database.Attributes.Column"}
    Assembly: {Rest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null}
    AssemblyQualifiedName: "Rest.Database.Attributes.Column, Rest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
    Attributes: Public | Sealed | BeforeFieldInit
    BaseType: {Name = "Attribute" FullName = "System.Attribute"}
    ContainsGenericParameters: false
    CustomAttributes: Count = 1
    DeclaredConstructors: {System.Reflection.ConstructorInfo[1]}
    DeclaredEvents: {System.Reflection.EventInfo[0]}
    DeclaredFields: {System.Reflection.FieldInfo[4]}
    DeclaredMembers: {System.Reflection.MemberInfo[13]}
    DeclaredMethods: {System.Reflection.MethodInfo[4]}
    DeclaredNestedTypes: {System.Reflection.TypeInfo.<get_DeclaredNestedTypes>d__23}
    DeclaredProperties: {System.Reflection.PropertyInfo[4]}
    DeclaringMethod: '((System.RuntimeType)(typeof(Column))).DeclaringMethod' threw an exception of type 'System.InvalidOperationException'
    DeclaringType: null
    FullName: "Rest.Database.Attributes.Column"
    GUID: {0f891941-d2ca-3a57-b5cc-054d94ec41f2}
    GenericParameterAttributes: '((System.RuntimeType)(typeof(Column))).GenericParameterAttributes' threw an exception of type 'System.InvalidOperationException'
    GenericParameterPosition: '((System.RuntimeType)(typeof(Column))).GenericParameterPosition' threw an exception of type 'System.InvalidOperationException'
    GenericTypeArguments: {System.Type[0]}
    GenericTypeParameters: {System.Type[0]}
    HasElementType: false
    ImplementedInterfaces: {System.Type[1]}
    IsAbstract: false
    IsAnsiClass: true
    IsArray: false
    IsAutoClass: false
    IsAutoLayout: true
    IsByRef: false
    IsCOMObject: false
    IsClass: true
    IsConstructedGenericType: false
    IsContextful: false
    IsEnum: false
    IsExplicitLayout: false
    IsGenericParameter: false
    IsGenericType: false
    IsGenericTypeDefinition: false
    IsImport: false
    IsInterface: false
    IsLayoutSequential: false
    IsMarshalByRef: false
    IsNested: false
    IsNestedAssembly: false
    IsNestedFamANDAssem: false
    IsNestedFamORAssem: false
    IsNestedFamily: false
    IsNestedPrivate: false
    IsNestedPublic: false
    IsNotPublic: false
    IsPointer: false
    IsPrimitive: false
    IsPublic: true
    IsSealed: true
    IsSecurityCritical: true
    IsSecuritySafeCritical: false
    IsSecurityTransparent: false
    IsSerializable: false
    IsSpecialName: false
    IsUnicodeClass: false
    IsValueType: false
    IsVisible: true
    MemberType: TypeInfo
    MetadataToken: 33554466
    Module (System.Reflection.MemberInfo): {Rest.dll}
    Module: {Rest.dll}
    Name: "Column"
    Namespace: "Rest.Database.Attributes"
    ReflectedType: null
    StructLayoutAttribute: {System.Runtime.InteropServices.StructLayoutAttribute}
    TypeHandle: {System.RuntimeTypeHandle}
    TypeInitializer: null
    UnderlyingSystemType: {Name = "Column" FullName = "Rest.Database.Attributes.Column"}

有人可以解释为什么会导致溢出异常吗?

提前致谢!

EDIT1:即使这会导致算术溢出x.GetCustomAttributes(typeof(Column)).Any()

EDIT2:Pastebin https://pastebin.com/raw/LQDmvU7J

EDIT3:TypeBuilder代码

    foreach (Type type in RestServer.CallerAssembly.GetTypes())
    {
        if (!typeof(IDataSet).IsAssignableFrom(type) && !type.IsInterface && type != typeof(DataSet<>) )
        {
            continue;
        }

        TypeBuilder builder = Builder.GetTypeBuilder(type.Name, type);
        Dictionary<string, PropertyInfo> properties = new Dictionary<string, PropertyInfo>();

        type.GetProperties()
            .Where(p => p.GetCustomAttributes(typeof(Relation), false).Length > 0)
            .ToList()
            .ForEach(x => properties.Add(OverrideRelation(x, ref builder, type), x));

        BuildClassTypes(builder, properties, type);
    }

我重新创建完全相同的类并创建新的Type。 在这部分OverrideRelation中,我添加了一个新属性,在我输入此内容时,我正在打击自己,因为我认为这就是我的错误....

            MethodBuilder propertyGetBuilder = builder.DefineMethod
            (
                dynamicFunctionName,
                MethodAttributes.Public,
                propertyInfo.PropertyType,
                Type.EmptyTypes
            );

            ILGenerator propertyGetIlGenerator = propertyGetBuilder.GetILGenerator();

            propertyGetIlGenerator.Emit(OpCodes.Ldarg_0);
            propertyGetIlGenerator.Emit(OpCodes.Ldstr, propertyInfo.Name);
            propertyGetIlGenerator.Emit(OpCodes.Ldstr, relationKeyField.Name);
            propertyGetIlGenerator.Emit(OpCodes.Ldstr, relationAttribute.RelationColumn);
            propertyGetIlGenerator.Emit(OpCodes.Call, loadRelationMethod);

            propertyGetIlGenerator.Emit(OpCodes.Ret);

            PropertyBuilder newProperty = builder.DefineProperty
            (
                propertyInfo.Name, 
                propertyInfo.Attributes, 
                propertyInfo.PropertyType, null
            );

            newProperty.SetSetMethod(propertyGetBuilder);

0 个答案:

没有答案