Tag: custom attributes

如何将编译器检查的属性名称/表达式树传递给自定义属性

在一些地方,我注意到表达式树作为参数传递给方法,以允许编译器检查属性名称。 例如,Caliburn Micro在其PropertyChangedBase类中具有以下方法签名: public virtual void NotifyOfPropertyChange(Expression<Func> property); 我有一个自定义属性,我想在构造函数中使用相同类型的编译器检查属性名称,以使我能够键入: [MyCustomAttribute(() => PropertyName)] 代替: [MyCustomAttribute(“PropertyName”)] 使用构造函数定义: public MyCustomAttribute(params Expression<Func>[] properties) 但是,由于属性参数的限制是常量表达式,这似乎是不可能的。 任何人都可以推荐一种不同的方法,我可以让编译器检查我的属性参数中的属性名称,而不是留下这个只使用字符串的潜在错误? 编辑:感谢Marc的回答,我现在已经实现了这个: #if DEBUG foreach (var propertyInfo in GetType().GetProperties().Where(propertyInfo => Attribute.IsDefined(propertyInfo, typeof (MyCustomAttribute)))) { foreach (var propertyName in propertyInfo.GetAttributes(true) .SelectMany(attribute => attribute.PropertyNames)) Debug.Assert( GetType().GetProperty(propertyName) != null, “Property not found”, “Property {0} declared on attributed property […]

Web.Config中的自定义部分/集合

我有一堆路由,我希望能够在我的Web.Config文件中抛出。 我需要集合中每个部分/项目的一个键和两个值字段。 有点像这样…… 这可能吗?

在自定义属性中传递自定义参数 – ASP.NET MVC

我的目标是创建一个自定义属性,如System.ComponentModel.DataAnnotations.Display,它允许我传递一个参数。 例如:在System.ComponentModel.DataAnnotations.Display中,我可以将值传递给参数Name [Display(Name = “PropertyName”)] public int Property { get; set; } 我想做同样的事情,但是在下面的控制器和动作中 [CustomDisplay(Name = “Controller name”)] public class HomeController : Controller 然后使用其值填充ViewBag或ViewData项。 有人可以帮我弄这个吗? 谢谢。

如何检查C#类是否使用了安全属性

如何检查并确保类使用我自己的自定义安全属性? 我知道我可以使用reflection来获取普通属性,但如果自定义属性基于安全属性,如下所示,则reflection不会显示它。 有没有办法检查? 为什么我需要这样做是为了确保加载到基于云的系统的插件必须使用安全属性,以便加载的类无法访问任何受限文件等等。 这是我正在使用的自定义安全类: public class PluginSection : CodeAccessSecurityAttribute { public PluginSection(SecurityAction action) : base(action) { } public override IPermission CreatePermission() { // WebSites.GetInstance().LocalBaseDir returns the base directory where the class has accesss to login return new FileIOPermission(FileIOPermissionAccess.Write, WebSites.GetInstance().LocalBaseDir); } } 我必须使用基于CodeAccessSecurityAttribute的类,以便FileIOPermission可以工作。 此外,如果有另一种方法来限制正在加载的插件的访问,我也可以使用它。

c#:什么是常量表达式?

我目前正在使用属性。 我经常遇到错误’属性参数必须是属性参数类型的常量表达式,typeof表达式或数组创建表达式。 我真的不知道’常数表达’是什么意思。 能够将Func传递给属性(由属性存在时执行的代码所使用)将非常有用。 但是,唉,没有。 我不明白为什么这种类型不能放在程序集元数据中,我认为这是我无法将其传递给属性的原因。 任何人都可以给我任何想法吗?

你可以将属性应用于C#中的多个字段吗?

这似乎不太可能,但无论如何我都会问……在C#中是否可以同时将单个属性应用于多个字段? public class MyClass { [SomeAttribute] public int m_nVar1; [SomeAttribute] public int m_nVar2; public int m_nVar3; } 是否有一个简单的方法将“SomeAttribute”放在m_Var1和m_Var2上,而不是放在m_nVar3上? 目前,我们将属性放在每个字段之前,但是将所有字段放在块中使用属性会很好。

如何定义命名参数C#

这似乎是一个简单的问题,但由于某种原因,我无法在任何地方找到答案。 基本上,我希望能够实现一个带NamedParameters的构造函数 。 通过命名参数,我不是指具有默认值(可选参数)的参数,例如: public SomeMethod(){ string newBar = Foo(bar2 : “customBar2”); } public string Foo(string bar1 = “bar1”, bar2 = “bar2” ){ //… } 我想要实现的一个很好的例子是System.Web.Mvc程序集中的AuthorizeAttribute 。 您可以使用以下方式: [Authorize(Roles = “Administrators”, Users = “ThatCoolGuy”)] public ActionResult Admin(){ } intellisense中构造函数的签名类似于以下示例,我相信(请确认)那些NamedParameters正在映射到类属性。 AuthorizeAttribute.AuthorizeAttribute( NamedParameters … )Initiliaze System.Web.Mvc.AuthorizeAttribute类的新实例 命名参数: 订单int 用户字符串 角色字符串

如何使用方法参数属性

我一直在努力寻找如何编写自定义属性来validation方法参数的示例,即转换此表单: public void DoSomething(Client client) { if (client.HasAction(“do_something”)) { // … } else { throw new RequiredActionException(client, “do_something”); } } 进入这个: public void DoSomething([RequiredAction(Action=”some_action”)] Client client) { // … } 据我所知,我需要将此属性添加到我的自定义属性,但我对如何访问装饰参数Client感到茫然: [AttributeUsageAttribute(AttributeTargets.Parameter)] public class RequireActionAttribute : System.Attribute { public Type Action {get; set;} public RequireActionAttribute() { // .. How do you access the decorated parameter? […]

C#属性用法:仅允许具有特定数据类型的属性上的属性

我为属性创建了一些属性。 现在我想将这些属性限制为具有特定数据类型的属性? 这个想法是,如果分配给不同的类型,将抛出编译器错误。 这可能吗? 如果没有,那么我想我将不得不在运行时检查它。

检索自定义属性参数值?

如果我创建了一个属性: public class TableAttribute : Attribute { public string HeaderText { get; set; } } 我适用于课堂上的一些属性 public class Person { [Table(HeaderText=”F. Name”)] public string FirstName { get; set; } } 在我的视图中,我有一个显示在表格中的人员列表..如何检索HeaderText的值以用作我的列标题? 就像是…