Tag: custom attributes

使用自定义属性获取程序集中的所有类型

是否有一种优雅的方法来获取具有自定义属性的程序集中的所有类型? 所以如果我有课 [Findable] public class MyFindableClass {} 我希望能够在Assembly.GetTypes(…)返回的类型集合中找到它。 我可以用一个很大的邪恶黑客做到这一点,但我确信有人有更好的方式。

如何将条件必需属性放入类属性以使用WEB API?

我只想放置与WEB API一起使用的条件必需属性 例 public sealed class EmployeeModel { [Required] public int CategoryId{ get; set; } public string Email{ get; set; } // If CategoryId == 1 then it is required } 我通过( ActionFilterAttribute )使用模型状态validation

如何从C#中的对象实例获取自定义属性

假设我有一个名为Test的类,其中一个名为Title的属性具有自定义属性: public class Test { [DatabaseField(“title”)] public string Title { get; set; } } 还有一个名为DbField的扩展方法。 我想知道是否可以在c#中从对象实例获取自定义属性。 Test t = new Test(); string fieldName = t.Title.DbField(); //fieldName will equal “title”, the same name passed into the attribute above 可以这样做吗?

在运行时更改自定义属性的参数

我需要在运行时更改属性的参数。 我将我的问题简化为简单的例子。 属性类: [AttributeUsage(AttributeTargets.Property)] public class MyAttribute : Attribute { public string Name { get; set; } } 简单的实体,它使用属性修饰了属性: public class MyEntity { [MyAttribute(Name=”OldValue1″)] public string Data1{ get; set; } [MyAttribute(Name = “OldValue2″)] public string Data2 { get; set; } } 我创建了MyEntity类的实例。 我可以更改对象属性的值,但是我不能在对象实体上更改属性的属性Name的值。 可能吗? 对象实体的属性值我可以用这部分代码改变: entityProp.SetValue(entity,”NewData”,null); 但我不知道如何更改对象实体上属性Name的值 这不起作用: attProp.SetValue(attribute,”NewData”,null); 属性的价值名称仍然是原创的。 这是所有测试代码。 谢谢你的帮助。 [TestMethod] public void […]

属性上的自定义属性 – 获取属性的类型和值

我有以下自定义属性,可以应用于属性: [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] public class IdentifierAttribute : Attribute { } 例如: public class MyClass { [Identifier()] public string Name { get; set; } public int SomeNumber { get; set; } public string SomeOtherProperty { get; set; } } 还有其他类,Identifier属性可以添加到不同类型的属性中: public class MyOtherClass { public string Name { get; set; } [Identifier()] public int […]

WCF服务属性用于记录方法调用和exception

我需要在WCF服务中记录每个方法调用,并抛出任何exception。 这导致了许多冗余代码,因为每个方法都需要包含类似于此的样板: [OperationContract] public ResultBase Add(int x, int y) { var parameters = new object[] { x, y } MyInfrastructure.LogStart(“Add”, parameters); try { // actual method body goes here } catch (Exception ex) { MyInfrastructure.LogError(“Add”, parameters, ex); return new ResultBase(“Oops, the request failed”, ex); } MyInfrastructure.LogEnd(“Add”, parameters); } 有没有办法可以将所有这些逻辑封装到MyServiceLoggingBehaviorAttribute属性中,我可以将其应用于服务类(或方法),如下所示: [ServiceContract] [MyServiceLoggingBehavior] public class MyService { […]

属性参数必须是常量表达式,… – 创建类型为array的属性

这是我的自定义属性和我正在使用它的类: [MethodAttribute(new []{new MethodAttributeMembers(), new MethodAttributeMembers()})] public class JN_Country { } public class MethodAttribute : Attribute { public MethodAttributeMembers[] MethodAttributeMembers { get; set; } public MethodAttribute(MethodAttributeMembers[] methodAttributeMemberses) { MethodAttributeMembers = methodAttributeMemberses; } } public class MethodAttributeMembers { public string MethodName { get; set; } public string Method { get; set; } public string MethodTitle { […]