Tag: custom attributes

属性和命名/可选构造函数参数不起作用

我有自定义属性,如下所示: [AttributeUsage(AttributeTargets.Field)] public class EnumDisplayAttribute : Attribute { public string Description { get; private set; } public string Code { get; private set; } public EnumDisplayAttribute(string description = null, string code = null) { Description = description; Code = code; } } 两个构造函数参数都是可选的。 在像这样的字段上使用此属性时 public enum TransactionType { [EnumDisplay(code: “B”)] Bill, [EnumDisplay(description: null, code: “C”)] […]

使用reflection获取MemberInfo的类型

我正在使用reflection来加载具有项目类结构的树视图。 类中的每个成员都有一个分配给它们的自定义属性。 我使用MemberInfo.GetCustomAttributes()获取类的属性没有问题,但是如果类成员是自定义类,然后需要解析自身以返回自定义属性,我需要一种方法。 到目前为止,我的代码是: MemberInfo[] membersInfo = typeof(Project).GetProperties(); foreach (MemberInfo memberInfo in membersInfo) { foreach (object attribute in memberInfo.GetCustomAttributes(true)) { // Get the custom attribute of the class and store on the treeview if (attribute is ReportAttribute) { if (((ReportAttribute)attribute).FriendlyName.Length > 0) { treeItem.Items.Add(new TreeViewItem() { Header = ((ReportAttribute)attribute).FriendlyName }); } } // PROBLEM HERE […]

类成员的自定义属性

我正在使用自定义属性来定义类的成员如何映射到属性以作为表单发布(支付网关)发布。 我有自定义属性工作正常,并能够通过“名称”获取属性,但希望由成员本身获取属性。 例如: getFieldName(“name”); VS getFieldName(obj.Name); 计划是编写一个方法,将带有成员的类序列化为postable字符串。 这是我此时的测试代码,其中ret是一个字符串,PropertyMapping是自定义属性: foreach (MemberInfo i in (typeof(CustomClass)).GetMember(“Name”)) { foreach (object at in i.GetCustomAttributes(true)) { PropertyMapping map = at as PropertyMapping; if (map != null) { ret += map.FieldName; } } } 提前致谢!

是否可以在编译期间(而不是运行时)在C#中查询自定义属性

换句话说,如果每个类都没有(“必须拥有”)自定义属性(例如作者和版本),是否可以创建甚至不编译的程序集(假设检查代码未被删除)? 这是我在运行时查询时使用的代码: using System; using System.Reflection; using System.Collections.Generic; namespace ForceMetaAttributes { [System.AttributeUsage ( System.AttributeTargets.Method, AllowMultiple = true )] class TodoAttribute : System.Attribute { public TodoAttribute ( string message ) { Message = message; } public readonly string Message; } [System.AttributeUsage ( System.AttributeTargets.Class | System.AttributeTargets.Struct, AllowMultiple = true )] public class AttributeClass : System.Attribute { public […]

覆盖属性设置器和getter

有没有办法覆盖具有属性的auto属性的setter和getter? 像这样: [CustomAttribute] public int Value { get; set; } … public class CustomAttibute : Attribute { public override NotExistingPropertySetter(object value) { if (((int)value) < 10 ) { value = 10; } } }

ContextBoundObject在等待之后引发远程处理错误

我有一些日志代码,使用ContextBoundObject和ContextAttribute编写拦截方法调用。 该代码基于代码项目示例 。 这一切都运行良好,直到我们开始使用此库与利用async和等待的代码。 现在我们在运行代码时遇到了远程错误。 这是一个重现问题的简单示例: public class OhMyAttribute : ContextAttribute { public OhMyAttribute() : base(“OhMy”) { } } [OhMy] public class Class1 : ContextBoundObject { private string one = “1”; public async Task Method1() { Console.WriteLine(one); await Task.Delay(50); Console.WriteLine(one); } } 当我们调用Method1我们在第二个Console.WriteLine上得到以下RemotingException : Remoting cannot find field ‘one’ on type ‘WindowsFormsApplication1.Class1’. 有没有办法使用内置的C#方法解决这个问题,还是我们必须看看像PostSharp这样的替代解决方案?

使用generics方法获取我的枚举属性的List

一开始,我们有这个基本的枚举。 public enum E_Levels { [ValueOfEnum(“Low level”)] LOW, [ValueOfEnum(“Normal level”)] NORMAL, [ValueOfEnum(“High level”)] HIGH } 我想得到一个List 无论枚举 。 类似Extensions.GetValuesOfEnum() ,它可以返回List其中包含“低级别”,“正常级别”和“高级别”。 StackOF帮助我获得了一个值属性: public static class Extensions { public static string ToValueOfEnum(this Enum value) { FieldInfo fieldInfo = value.GetType().GetField(value.ToString()); ValueOfEnum[] attribs = fieldInfo.GetCustomAttributes(typeof(ValueOfEnum), false) as ValueOfEnum[]; return attribs.Length > 0 ? attribs[0].value : null; } } 无论枚举如何,我都可以调用此方法: […]

为.NET属性目标指定所需的基类

我尝试使用下面的代码创建一个自定义.NET属性,但不小心忽略了子类。 这会在注释中生成一个容易修复的编译器错误。 // results in compiler error CS0641: Attribute ‘AttributeUsage’ is // only valid on classes derived from System.Attribute [AttributeUsage(AttributeTargets.Class)] internal class ToolDeclarationAttribute { internal ToolDeclarationAttribute() { } } 我的问题是编译器如何知道[AttributeUsage]属性只能应用于System.Attribute的子类? 使用.NET Reflector我没有在AttributeUsageAttribute类声明本身上看到任何特殊内容。 不幸的是,这可能只是编译器本身生成的特殊情况。 [Serializable, ComVisible(true), AttributeUsage(AttributeTargets.Class, Inherited=true)] public sealed class AttributeUsageAttribute : Attribute { … 我希望能够指定我的自定义属性只能放在特定类(或接口)的子类上。 这可能吗?

Lambda的属性构造函数

可以这样做: public static void SomeMethod(Expression expr) { //LambdaExpression happily excepts any Expession LambdaExpression lamb = expr; } 并在其他地方调用它传递参数的lambda: SomeMethod<Func<IQueryable,Person>>( p=>p.FirstOrDefault()); 我想改为将表达式作为参数传递给属性构造函数 。 可以这样做吗? class ExpandableQueryAttribute: Attribute { private LambdaExpression someLambda; //ctor public ExpandableQueryMethodAttribute(LambdaExpression expression) { someLambda = expression } } //usage: static LambdaExpression exp = (Expression<Func<IQueryable, Person>>) (p => p.FirstOrDefault()); [ExpandableQueryAttribute(exp)] //error here // “An […]

生成自定义编译时警告C#

我正在使用VS2008,并希望根据属性上的自定义属性创建编译时警告/错误(如果可能)。 目前有两个案例让我感兴趣: [MyAttribute (typeof(MyClass)] MyClass必须实现一个接口。 目前我在属性的构造函数中声明了这一点,但是由于堆栈跟踪的性质,这不容易跟踪: public MyAttribute (Type MyClassType) { System.Diagnostics.Debug.Assert(typeof(MyInterface).IsAssignableFrom(MyClassType), “Editor must implement interface: ” + typeof(MyInterface).Name); } 我感兴趣的第二种情况是我在属性中定义了一个类型,如果该类型实现了一个接口,那么如果另一个属性不存在则应该显示警告。 IE if(MyClass.Implements(SomeInterface)&&!Exists(SomeAttibute)){Generate Warning} [MyAttribute(typeof(MyClass)] // Comment next line to generate warning [Foo (“Bar”)] 谢谢!