类实例传递的参数属性构造 [英] Pass instance of Class as parameter to Attribute constructor

查看:112
本文介绍了类实例传递的参数属性构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的类/模型的实例(用于访问非静态成员的目的)在我的自定义属性。

I need an instance of class/model(for the purpose of accessing a non-static member) within my custom attribute.

public class LoginModel
{
     [AutoComplete(currentInstance)]  //pass instance of class or CompanyNames
     public string DepartmentName { get; set; }


     public string[] DepartmentNames { get {...} }
}

有没有办法做到这一点,而无需使用新()或反思。

推荐答案

这是完全不可能的。属性被烤成装配在编译时的元数据,以便在谈论一个合格的的一个属性的情况下没有任何意义,因为情况下,只有在运行时存在。

That's totally impossible. Attributes are baked into the metadata of the assembly at compile-time so talking about passing an instance of a class to an attribute doesn't make any sense because instances exist only at runtime.

在另一方面属性始终反射消耗,所以我想,在那一刻你检查的类此的元数据自定义属性,你可以使用该实例的presence。

On the other hand attributes are always consumed by reflection, so I guess that at the moment you are checking for the presence of this custom attribute on the class metadata you could use the the instance.

这篇关于类实例传递的参数属性构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆