Tag: propertyinfo

如何获取具有指定名称的DataMemberAttribute的属性?

如何reflection性地获取具有给定名称的DataMember的属性(让我们假设每个DataMember都有一个唯一的名称)? 例如,在以下代码中,具有名称“p1”的DataMember的属性是PropertyOne : [DataContract(Name = “MyContract”)] public class MyContract { [DataMember(Name = “p1”)] public string PropertyOne { get; set; } [DataMember(Name = “p2”)] public string PropertyTwo { get; set; } [DataMember(Name = “p3”)] public string PropertyThree { get; set; } } 目前,我有: string dataMemberName = …; var dataMemberProperties = typeof(T).GetProperties().Where(p => p.GetCustomAttributes(typeof(DataMemberAttribute), false).Any()); var propInfo […]

如何将包含某些属性的custom-class类型的未知对象列表传递给方法?

我正在创建一个databasehelper类,其中包含访问SQLCE数据库的方法。 我想使用相同的方法使用包含与不同表中的字段匹配的属性的不同类来读取行。 要使用的类是在运行时确定的,我想将包含类中对象的列表传递给方法,并获取属性名并使用它们来读取数据库。 会非常方便,因为我可以将它用于我的所有(SQLCE-)数据库。 (我更新了错误的代码以便在这里提供解决方案) #region ReadData ///———————————————————————- /// /// Reads datarows from database and adds them to list. /// /// List containing objects with properties. /// Table in database. /// Substring of SQL-statement that follows ‘WHERE’. /// Connectionstring. /// true if successfull ///———————————————————————- public static bool ReadData(List data, string table, string search, string connect) […]

将属性传递给C#中的方法

我需要传递某些类型的属性选择(每次一种类型),假设这是我的类型: public class Product { [PrimaryKey] public long Id { get; set; } [DisplayName(“Name”)] public string Title { get; set; } [Foreignkey(Schema = “Products”, Table = “MajorCategory”, Column = “Id”)] [DisplayName(“MCat”)] public string MajorCategory { get; set; } [Foreignkey(Schema = “Products”, Table = “Category”, Column = “Id”)] [DisplayName(“Cat”)] public string Category { get; set; } […]

如何通过reflection区分值类型,可空值类型,枚举,可空枚举,引用类型?

如何通过reflection区分值类型,可空值类型,枚举,可空枚举,引用类型? enum MyEnum { One, Two, Three } class MyClass { public int IntegerProp { get; set; } public int? NullableIntegerProp { get; set; } public MyEnum EnumProp { get; set; } public MyEnum? NullableEnumProp { get; set; } public MyClass ReferenceProp { get; set; } } class Program { static void Main(string[] args) { […]

从对象获取属性信息,而不将属性名称作为字符串

出于某些原因,我需要创建一个PropertyInfo实例的Dictionary,它对应于某些类的属性(让我们称之为EntityClass )。 好的,我可以使用typeof(EntityClass).GetProperties() 。 但我还需要确定某些特定属性的值(在编译时已知)。 通常我可以做以下其中一项: EntityInstance.PropertyX = Value; typeof(EntityClass).GetProperty(“PropertyX”).SetValue(EntityInstance, Value, null); 为了填满我的字典,我需要使用PropertyInfo实例而不是正常设置值。 但我觉得通过字符串名称获取属性并不舒服。 如果某些EntityClass发生更改,则会带来许多exception而不是编译错误。 所以,我要问的是: 如何在不传递字符串名称的情况下获取已知属性的PropertyInfo? 如果有像代表一样的话,我会很高兴: SomeDelegateType MyDelegate = EntityInstance.MethodX; 理想的情况是: SomePropertyDelegate MyPropertyDelegate = EntityInstance.PropertyX;

以编程方式获取entity framework中的Property的’Nullable’状态

我需要为EF中的字段获取Nullable属性。 一些魔法代码需要在Nullable = True的属性上完成,我找不到一个有效的解决方案来获取属性。 foreach (PropertyInfo property in (PropertyInfo[])type.GetProperties()) { var getPropertyType = property.GetMethod.ReturnTypeCustomAttributes.ToString(); var getValue = property.GetValue(model); bool isNullable = property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable); // isNullable always returns false !!!! and I need it to return true if the field is allowed to be null if ((getValue == null) && (!isNullable)) { } […]

使用包含另一个对象数组的对象的reflection读取属性

如何使用c#中的reflection读取包含数组类型元素的对象的属性。 如果我有一个名为GetMyProperties的方法,并且我确定该对象是自定义类型,那么我该如何读取数组的属性及其中的值。 IsCustomType是确定类型是否为自定义类型的方法。 public void GetMyProperties(object obj) { foreach (PropertyInfo pinfo in obj.GetType().GetProperties()) { if (!Helper.IsCustomType(pinfo.PropertyType)) { string s = pinfo.GetValue(obj, null).ToString(); propArray.Add(s); } else { object o = pinfo.GetValue(obj, null); GetMyProperties(o); } } } 场景是,我有一个ArrayClass和ArrayClass的对象有两个属性: -string Id -DeptArray[] depts DeptArray是另一个具有2个属性的类: -string code -string value 因此,此方法获取ArrayClass的对象。 我想在字典/列表项中读取所有属性从上到下并存储名称/值对。 我能够为价值,定制,枚举类型做到这一点。 我被一堆物体困住了。 不知道怎么做。

C#使用reflection开发.Net3.5来获取/设置嵌套属性和/或嵌套字段的值

我正在开发一个适用于从基类inheritance的数据块类的应用程序,我正在尝试使用Reflection深入到我的数据块类中的属性/字段。 由于所有数据块类都是从基类派生/inheritance的(包含Size属性),我可以使用类型基类的通用变量来轻松地在我的应用程序中创建一个对象; 我也可以在顶层获得/设置属性。 我的问题发生在属性是一个字段时 – 我不知道如何进入字段中的下一级别以获取基本属性和/或字段(如果适用)。 我的基础类: namespace MyBase { public class BaseClass { private int _size; public BaseClass() { } public BaseClass(int size) { _size = size; } public int Size() { return _size; } } } 数据块类#1: namespace DataBlock_class { //Data block class #1: (contains simple properties – will be used later) public […]

如何使用C#Reflection将Vaues设置为嵌套属性。?

我试图使用reflection动态地将值设置为类的嵌套属性。 任何人都可以帮我这样做。 我有一个类Region如下所示。 public class Region { public int id; public string name; public Country CountryInfo; } public class Country { public int id; public string name; } 我有一个Oracle Data reader来提供Ref游标的值。 这会给我一个 ID,姓名,COUNTRY_ID,COUNTRY_NAME 我可以通过下面的方式将值分配给Region.Id,Region.Name。 FieldName=”id” prop = objItem.GetType().GetProperty(FieldName, BindingFlags.Public | BindingFlags.Instance); prop.SetValue(objItem, Utility.ToLong(reader_new[ResultName]), null); 对于嵌套属性,我可以通过读取Fieldname创建实例来为下面的值赋值。 FieldName=”CountryInfo.id” if (FieldName.Contains(‘.’)) { Object NestedObject = objItem.GetType().GetProperty(Utility.Trim(FieldName.Split(‘.’)[0]), BindingFlags.Public | […]

获取属性名称的扩展方法

我有一个扩展方法来获取属性名称 public static string Name(this Expression<Func> expression) { MemberExpression body = (MemberExpression)expression.Body; return body.Member.Name; } 我称之为 string Name = ((Expression<Func>)(() => this.PublishDateTime)).Name(); 这工作正常,并将PublishDateTime作为字符串返回给我。 但是我对调用语句有一个问题,它看起来太复杂了,我想要这样的东西。 this.PublishDateTime.Name() 有人可以修改我的扩展方法吗?