Is it possible in C # to get attributes attached to a property from an instance of this property without knowing the containing class?

The name is a little tired, but easier to see in the code:

public struct MyStruct {
    public bool HasAttribute(Attribute attribute) {
        //is there any way to know?
        return ??????;
    }
}

public class MyClass {
    [SomeAttribute]
    MyStruct child;

    public MyClass() {}
}

I already know how to find attributes on MyClass.childby getting property information for each of my properties and then calling GetCustomAttributes, but this only works if I know that the struct instance matches MyClass.child. I would like to do here to find out if a particular instance of the structure has attributes associated with it, not knowing which class contains the particular instance.

It would be wise for me if you could not do this for reference types, because an instance can reference from several places, but should the attribute set always be well defined for value types?

, . , . , .

+1
1

, . [SomeAttribute] MyClass MyStruct.

:

  • , , , CPU. , .

  • , .

  • , , . , .

    : . - vtable (, , vtable ). / , , . - .

    , , GC , , .

API CLR, , Cthulhu.

, , , - . , :

public class MyClass {
    MyStruct child = new MyStruct(doThatSpecialThingie: true);
}
+2

Source: https://habr.com/ru/post/1627671/


All Articles