How to get a custom attribute from object instance in C#

前端 未结 6 1208
眼角桃花
眼角桃花 2020-12-05 07:40

Let\'s say I have a class called Test with one property called Title with a custom attribute:

public class Test
{
    [DatabaseField(\"title\")]
    public s         


        
6条回答
  •  醉话见心
    2020-12-05 07:56

    In order to get the attribute value, you need the type that the attribute applies to. Your extension method is only getting a string value (the value of Title), so you would not be able to get the actual instance that the string came from, and so you can't get the original type that the Title property belongs to. This will make it impossible to get the attribute value from your extension method.

提交回复
热议问题