Tag: 自定义属性

.net处理exception的属性 – 在属性访问器上使用

我从asp.net mvc经验中知道你可以拥有处理exception的属性(HandleErrorAttribute)。 据我所知,Controller类有一些OnException事件,这可能是这种行为的组成部分。 但是,我想在我自己的代码中做类似的事情: 梦想的例子: public String MyProperty { [ExceptionBehaviour(typeof(FormatException), MyExEnum.ClearValue)] set { _thing.prop = Convert.ToThing(value); } } …. 上面的代码显然没什么意义,但接近我想做的事情。 我希望属性集访问器上的属性捕获某种类型的exception,然后以某种自定义方式处理它(或者甚至只是吞下它)。 任何想法的家伙?

C#.NET CORE如何获取自定义属性的值?

我有一个自定义属性类定义如下。 [AttributeUsage(AttributeTargets.Property, Inherited = false)] internal class EncryptedAttribute : System.Attribute { private bool _encrypted; public EncryptedAttribute(bool encrypted) { _encrypted = encrypted; } public virtual bool Encrypted { get { return _encrypted; } } } 我将以上属性应用于另一个类,如下所示。 public class KeyVaultConfiguration { [Encrypted(true)] public string AuthClientId { get; set; } = “”; public string AuthClientCertThumbprint { get; set; […]

控制器function上方的自定义属性

我想将[FirstTime]属性置于控制器函数之上,然后创建一个FirstTimeAttribute ,它具有一些逻辑,用于检查用户是否输入了他的名字,如果没有, /Home/FirstTime其重定向到/Home/FirstTime 。 所以不要做: public ActionResult Index() { // Some major logic here if (…) return RedirectToAction(“FirstTime”, “Home”); return View(); } 我会这样做: [FirstTime] public ActionResult Index() { return View(); } 这可能吗?

从自定义属性装饰属性获取价值?

我编写了一个我在类的某些成员上使用的自定义属性: public class Dummy { [MyAttribute] public string Foo { get; set; } [MyAttribute] public int Bar { get; set; } } 我能够从类型中获取自定义属性并找到我的特定属性。 我无法弄清楚怎么做是获取指定属性的值。 当我获取Dummy的实例并将其作为对象传递给我的方法时,如何获取我从.GetProperties()获取的PropertyInfo对象并获取分配给.Foo和.Bar的值? 编辑: 我的问题是我无法弄清楚如何正确调用GetValue。 void TestMethod (object o) { Type t = o.GetType(); var props = t.GetProperties(); foreach (var prop in props) { var propattr = prop.GetCustomAttributes(false); object attr = (from row […]

.NET Flag Enum从值中获取属性

问候StackOverflow, 如果我有一个带有Flag属性的枚举类型以及此枚举类型中具有各自属性的值,我该如何检索所有适当的属性? 例如: [Flags()] enum MyEnum { [EnumDisplayName(“Enum Value 1”)] EnumValue1 = 1, [EnumDisplayName(“Enum Value 2”)] EnumValue2 = 2, [EnumDisplayName(“Enum Value 3”)] EnumValue3 = 4, } void Foo() { var enumVar = MyEnum.EnumValue2 | MyEnum.EnumValue3; // get a collection of EnumDisplayName attribute objects from enumVar … }

ASP MVC C#:是否可以将动态值传递给属性?

好吧,我是C#的新手,我正在尝试使用ASP MVC2创建一个小网站。 我想创建自己的授权属性。 但如果可能,我需要传递一些值。 例如: [CustomAuthorize(GroupID = Method Parameter?] public ActionResult DoSomething(int GroupID) { return View(“”); } 我想授权访问页面。 但它取决于传递给控制器​​的值。 因此授权取决于groupID。 这有可能以任何方式实现这一目标吗? 提前致谢。

.NET,C#:如何添加充当ISerializable接口的自定义序列化属性

我正在做一些db linq对象的序列化,它包含EntitySet和EntityRef类。 我找到了一种非常简单的方法来处理这些类的序列化,只需使用ISerializable来正确处理这种类型的成员(将它们转换为序列化列表,并在反序列化时撤消它)。 但是,如果我能这样做,那将是非常好的: [Serializable] [SerializeLinqEntities] partial class Person { … } 代替: partial class Person : ISerializable { public virtual void GetObjectData( SerializationInfo si, StreamingContext ctxt ) { EntitySerializer.Serialize(this, typeof(Person), si, ctxt); } protected Person( SerializationInfo si, StreamingContext ctxt ) { EntitySerializer.Deerialize(this, typeof(Person), si, ctxt); } } 有没有办法做到这一点? 我查看了序列化类,似乎无法找到任何方法来设置自定义序列化filter例程(我可以在那里查找我的自定义属性)。 谢谢!

在使用JSON.NET序列化对象时,如何添加自定义根节点?

我已经为我的一些对象添加了一个自定义属性,如下所示: [JsonCustomRoot(“status”)] public class StatusDTO { public int StatusId { get; set; } public string Name { get; set; } public DateTime Created { get; set; } } 属性非常简单: public class JsonCustomRoot :Attribute { public string rootName { get; set; } public JsonCustomRoot(string rootName) { this.rootName = rootName; } } 序列化对象实例时JSON.NET的默认输出是: {“StatusId”:70,”Name”:”Closed”,”Created”:”2012-12-12T11:50:56.6207193Z”} 现在的问题是: 如何使用自定义属性的值向JSON添加根节点,如下所示 : […]

命名参数类型约束

我正在设计一个自定义属性类。 public class MyAttr: Attribute { public ValueRange ValRange { get; set; } } 然后我试图将此属性分配给相邻类中的属性: public class Foo { [MyAttr(ValRange= new ValueRange())] public string Prop { get; set; } } 但是,编译器抱怨如下: ‘ValRange’不是有效的命名属性参数,因为它不是有效的属性参数类型 我也尝试将ValueRange类转换为struct ,希望成为值类型可以解决问题。 有没有办法解决?

Attribute.IsDefined没有看到使用MetadataType类应用的属性

如果我通过MetadataType属性将属性应用于部分类,则不会通过Attribute.IsDefined()找到这些属性。 任何人都知道为什么,或者我做错了什么? 下面是我为此创建的测试项目,但我真的尝试将自定义属性应用于LINQ to SQL实体类 – 就像这个问题中的答案一样。 谢谢! using System; using System.ComponentModel.DataAnnotations; using System.Reflection; namespace MetaDataTest { class Program { static void Main(string[] args) { PropertyInfo[] properties = typeof(MyTestClass).GetProperties(); foreach (PropertyInfo propertyInfo in properties) { Console.WriteLine(Attribute.IsDefined(propertyInfo, typeof(MyAttribute))); Console.WriteLine(propertyInfo.IsDefined(typeof(MyAttribute), true)); Console.WriteLine(propertyInfo.GetCustomAttributes(true).Length); // Displays: // False // False // 0 } Console.ReadLine(); } } [MetadataType(typeof(MyMeta))] public partial […]