查看属性是否应用于类属性中 String 类型的属性

发布于 2025-01-04 01:29:48 字数 946 浏览 1 评论 0原文

假设我有属性:

public class Column_Attribute : Attribute
{
    public string DbType { get; set; }
    public bool IsPrimaryKey { get; set; }
}

那么我可以将该属性应用于属性,如下所示:

 [Column_Attribute(DbType = "Integer", IsPrimaryKey = true)]
 public int Id { get; set; }   

现在如何从属性类获取有关属性 Id 的信息。换句话说,我想做类似的事情:

public class Column_Attribute : Attribute
{
    // constructor
    public Column_Attribute(){
      // if the property has the name Id do something...
      // OR
      // if this is an attribute of a property do something
      // if this is an attribute of a field do something else

      // If this attribute is targeting a property that is a string do something
    }

    public string DbType { get; set; }
    public bool IsPrimaryKey { get; set; }
}

我实际上需要知道该属性是否应用于字符串属性。

我知道如何通过反射来做到这一点,但我想这样做属性类内部。这可能吗。希望我能正确解释自己

say I have the attribute:

public class Column_Attribute : Attribute
{
    public string DbType { get; set; }
    public bool IsPrimaryKey { get; set; }
}

then I can apply that attribute to a property as:

 [Column_Attribute(DbType = "Integer", IsPrimaryKey = true)]
 public int Id { get; set; }   

Now how can I get information about the property Id from the attribute class. In other words I want to do something like:

public class Column_Attribute : Attribute
{
    // constructor
    public Column_Attribute(){
      // if the property has the name Id do something...
      // OR
      // if this is an attribute of a property do something
      // if this is an attribute of a field do something else

      // If this attribute is targeting a property that is a string do something
    }

    public string DbType { get; set; }
    public bool IsPrimaryKey { get; set; }
}

I actually need to know if the attribute is being applied to a property that is a string.

I know how to do that with reflection but I want to do that inside the attribute class. Is that possible. Hope I am explaining myself correctly

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柏林苍穹下 2025-01-11 01:29:48

如果没有反射,则无法执行此操作,因为构造函数中的代码只有调用 GetCustomAttributes() 才会执行,这是反射的一部分。

请参阅 http://msdn.microsoft.com/ en-us/library/z919e8tw(v=vs.80).aspx

在 SampleClass 上调用 GetCustomAttributes 会导致 Author 对象
如上所述构建和初始化

如果您希望属性类包含处理代码,您可以创建一个接收属性名称的方法。属性名称将在调用 GetCustomAttributes() 时可用。

You cannot do that without reflection because the code in the constructor will not be executed until you call GetCustomAttributes(), which is a part of reflection.

see http://msdn.microsoft.com/en-us/library/z919e8tw(v=vs.80).aspx

Calling GetCustomAttributes on SampleClass causes an Author object to
be constructed and initialized as above

If you want your attribute class to contain the processing code, you could create a method receiving the property name. The property name will be available at the time of calling GetCustomAttributes().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文