Tag: 属性

在运行时修改类属性

我不确定我是否有可能看到: 在运行时更改Attribute的参数。 我的情况非常相似,但我试图在运行时更改类的属性: [Category(“Change me”)] public class Classic { public string Name { get; set; } } 其中一个答案是: Dim prop As PropertyDescriptor = TypeDescriptor .GetProperties(GetType(UserInfo))(“Age”) Dim att As CategoryAttribute = DirectCast( prop.Attributes(GetType(CategoryAttribute)), CategoryAttribute) Dim cat As FieldInfo = att.GetType.GetField( “categoryValue”, BindingFlags.NonPublic Or BindingFlags.Instance) cat.SetValue(att, “A better description”) 更改为更易阅读的格式,感谢Marc Gravell: TypeDescriptor.AddAttributes(table, new Category{ Name = “Changed” […]

System.Runtime.InteropServices.GuidAttribute是否用于除COM互操作之外的任何内容

我试图弄清楚为什么这个属性被添加到一个类。 Google在搜索时只会提供与COM相关的资料; 但该应用程序不通过COM做任何事情。 签到评论没有提供任何启示(它提到了另一个,同时做出的看似无关的变化); 也没有在更改的任何一方阅读我的电子邮件流量数天。

我如何记录ac#dll

如何编写类,以便在其他项目中引用dll的人可以看到属性和方法描述? [Description(“My age in years attribute”)] public int Age { get { return 0; } set { } } 不起作用,也不起作用 /// /// My age in years attribute /// public int Age { get { return 0; } set { } }

C#Properties,为什么在赋值之前检查是否相等

为什么我看到人们实现这样的属性? 检查该值是否等于当前值的重点是什么? public double? Price { get { return _price; } set { if (_price == value) return; _price = value; } }

将Class的实例作为参数传递给Attribute构造函数

我需要在我的自定义属性中使用类/模型的实例(用于访问非静态成员)。 public class LoginModel { [AutoComplete(currentInstance)] //pass instance of class or CompanyNames public string DepartmentName { get; set; } public string[] DepartmentNames { get {…} } } 有没有办法在不使用new()或Reflection的情况下执行此操作。

XmlRootAttribute是可inheritance的吗?

我有一个我用C#的XmlSerializer序列化的类。 它标有XmlRoot属性,我想在派生类中inheritance此属性。 查看文档时,它没有说XmlRoot使用AttributeUsageAttribute将Inherit设置为false(Inherit应该默认为true),但是在尝试反序列化没有XmlRoot属性的inheritance类时出错(“没想到。“)。 这目前有效: [Serializable()] [XmlRoot(“rootNode”)] public class BaseClass { [XmlAttribute(“attributeA”)] public int A { get; set; } } [Serializable()] [XmlRoot(“rootNode”)] public class InheritedClass : BaseClass { [XmlElement(“elementB”)] public int B { get; set; } } 这不起作用,但我想要的是: [Serializable()] [XmlRoot(“rootNode”)] public class BaseClass { [XmlAttribute(“attributeA”)] public int A { get; set; } } [Serializable()] public class […]

如何使用另一个也从查询中提取的对象的值来设置linq查询中所有项的属性?

我有一个从数据库中提取的查询: List items = new List(from i in context select new myClass { A = iA, B = “”, // i doesn’t know this, this comes from elsewhere C = iC } 我还有另一个查询做类似的事情: List otherItems = new List(from j in context select new myClass2 { A = jA, // A is the intersection, there will […]

c#隐藏类的基类的一些属性

我想知道是否可以从派生类中隐藏基类属性: 例: class BaseDocument { public string DocPath{get; set;} public string DocContent{get; set;} } class DerviedDocument: BaseDocument { //this class should not get the DocContent property public Test() { DerivedDocument d = new DerivedDocument(); d.//intellisense should only show me DocPath //I do not want this class to see the DocContent property } } 我不能将DocContent属性设为私有,因为我想在其他地方实例化BaseDocument类并在那里使用该属性。 无论如何,这将扼杀财产的想法。 […]

重写属性上的.net XmlSerializer

我有一个带有抽象属性的基类: public abstract int ID {get;set;} 现在,我有一个子类,它是XmlSerialized。 所以它有: [XmlElement(“something”)] public override int ID { get { //… } set { //… } } 我无法将XmlElement属性移动到baseclass,因为每个子类都有不同的xml元素名称。 现在,当我反序列化这个类时,我收到以下错误: 成员’Subclass.ID’隐藏inheritance的成员’BaseClass.ID’,但具有不同的自定义属性。 我能做什么?

具有“具有属性X”约束的generics函数?

我有一个第三方闭源应用程序,它导出一个COM接口,我通过Interop在我的C#.NET应用程序中使用它。 此COM接口导出许多对象,这些对象都显示为System.Object,直到我将它们转换为适当的接口类型。 我想分配所有这些对象的属性。 从而: foreach (object x in BigComInterface.Chickens) { (x as Chicken).attribute = value; } foreach (object x in BigComInterface.Ducks) { (x as Duck).attribute = value; } 但是分配属性很可能(由于特定于应用程序的原因,不可避免)抛出我想要恢复的exception,所以我真的想要一个try / catch。 从而: foreach (object x in BigComInterface.Chickens) { try { (x as Chicken).attribute = value; } catch (Exception ex) { // handle… } } foreach […]