Tag: 属性

通过递归遍历所有属性的属性来比较两个对象?

我已经编写了一个测试方法,用于比较一个类的两个实例(给出了类型兼容性的假设)。 我自豪地检查了所有的公共财产,确保返回一个差异列表。 问题是某些属性是包含自己属性的对象(子属性,如果可以的话)。 通过逐步完成流程,我可以看到这些没有被比较。 如何设计深入调用并比较所有子属性的调用? 如果方法相对简单,则额外奖励。 🙂 public static class Extensions { public static IEnumerable DiffersOn( this Generic self, Generic another) where Generic : class { if (self == null || another == null) yield return null; Type type = typeof(Generic); IEnumerable properties = type.GetProperties( BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo property in properties) { var […]

如何从C#(2.0)中的属性获取属性的名称

我知道我可以有一个属性,但这比我想去的工作更多……而且不够通用。 我想做点什么 class Whotsit { private string testProp = “thingy”; public string TestProp { get { return testProp; } set { testProp = value; } } } … Whotsit whotsit = new Whotsit(); string value = GetName(whotsit.TestProp); //precise syntax up for grabs.. 在哪里我期望价值等于“TestProp” 但我不能为我的生活找到正确的reflection方法来编写GetName方法… 编辑:我为什么要这样做? 我有一个类来存储从’name’,’value’表中读取的设置。 这由基于reflection的通用方法填充。 我非常想反过来写… /// /// Populates an object from a […]

内部使用属性有什么好处?

当从类外部访问成员时,封装显然是有用且必不可少的,但是当在内部引用类变量时,调用其私有成员或使用其getter更好吗? 如果你的getter只是返回变量, 那么性能是否存在差异 ?

以编程方式更改AssemblyVersion和AssemblyFileVersion属性

在设置创建过程中,我计划执行以下操作(在C#脚本中 ): 阅读一个DLL的AssemblyVersion和AssemblyFileVersion属性值。 迭代项目中的所有DLL和可执行文件,并将这些版本应用于其AssemblyVersion和AssemblyFileVersion属性值。 现在的问题是: 如何做第2步? 我成功地可以做第1步,但对于第2步,我没有看到真正的起点。 我可能要做的是使用一些本机P / Invoke方法,因为属性直接修改DLL /可执行文件的版本信息资源信息。 有关于此的任何提示吗?

检测类属性是否为引用类型

是否有可能在查看类的属性时检测它们中的任何一个是否为引用类型。 以下面为例: public class Client { public int Id { get; set; } public string Name { get; set; } } public class ProgrammeClient { public int Id { get; set; } public bool IsActive { get; set; } public IClient Client { get; set; } } ProgrammeClient: – Id和IsActive是属性,但Client是引用类型。 有没有办法检测到这个? 非常感谢,Kohan。 附录 我问的原因是:我使用的映射器在匹配属性名称和复制值之前检查类型是否相同。 我希望检测类并覆盖类型匹配,如果THEY类型匹配,只需复制类属性。

C#中公共get / protected set属性的Objective-C等价物是什么

有没有办法在Objective-C中创建像这个C#属性的属性? public int prop { get; protected set;} 从本质上讲,我希望能够从类外获取值,但只能从类中设置值。

如何使用reflection查找数据注释属性及其参数

我有一些数据注释属性,如下所示: [StringLength(20, MinimumLength = 5, ErrorMessage = “First name must be between 5 and 20 characters”)] 如何使用reflection查找数据注释属性及其参数? 谢谢

C# – 将属性值从一个实例复制到另一个实例,不同的类

我有两个C#类,它们具有许多相同的属性(按名称和类型)。 我希望能够将Defect实例中的所有非空值复制到DefectViewModel的实例中。 我希望用reflection来做,使用GetType().GetProperties() 。 我尝试了以下方法: var defect = new Defect(); var defectViewModel = new DefectViewModel(); PropertyInfo[] defectProperties = defect.GetType().GetProperties(); IEnumerable viewModelPropertyNames = defectViewModel.GetType().GetProperties().Select(property => property.Name); IEnumerable propertiesToCopy = defectProperties.Where(defectProperty => viewModelPropertyNames.Contains(defectProperty.Name) ); foreach (PropertyInfo defectProperty in propertiesToCopy) { var defectValue = defectProperty.GetValue(defect, null) as string; if (null == defectValue) { continue; } // “System.Reflection.TargetException: Object […]

为.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 { … 我希望能够指定我的自定义属性只能放在特定类(或接口)的子类上。 这可能吗?

我可以在运行时向对象属性添加属性吗?

例如,我想删除或更改下面的属性属性或添加一个新属性。 可能吗? [XmlElement(“bill_info”)] [XmlIgnore] public BillInfo BillInfo { get { return billInfo; } set { billInfo = value; } }