propertyinfo

How to get the default value of an object property? [duplicate]

人走茶凉 提交于 2019-12-11 03:51:49
问题 This question already has answers here : Programmatic equivalent of default(Type) (13 answers) Closed 6 years ago . Some code: foreach (System.Reflection.PropertyInfo pi in myObject.GetType().GetProperties()) { if (pi.CanWrite) { object value = pi.GetValue(Properties, null); // if (value is not default) // { X.addAttribute(pi.Name, value); // } } } What I'm trying to do is not-call the line 'X.addAttribute...' if the property is at its DefaultValue. I assume there's some way of getting the

How to pass a list of unknown objects of type custom-class containing some properties to method?

梦想的初衷 提交于 2019-12-08 05:22:42
问题 I am making a databasehelper class with methods to access a SQLCE database. I want to use the same method to read row(s) using different classes containing properties that match the fields in the different tables. The class to be used is determined during runtime and I want to pass a list with objects from the class on to the method and get the propertynames and use them to read the database. Would be very handy because I could use it for all my (SQLCE-)databases. (I updated the erroneous

Pass Property to the Method in C#

此生再无相见时 提交于 2019-12-08 01:06:31
问题 I need to pass selection of properties of some types(one type each time), assume this is my type: 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; } public long

Is there a way to get entity id-field's name by reflection or whatever?

回眸只為那壹抹淺笑 提交于 2019-12-06 13:58:45
问题 I am trying to get the ID field name (property name) of an entity, is it possible? User user= new User(); //User is an Entity string idField = ??????? //user.UserId 回答1: If you can get the EntitySet, or the EntityType, for the entity, then you can use the KeyMembers property: public IEnumerable<string> GetIdProperties(EntitySetBase entitySet) { return GetIdProperties(entitySet.ElementType); } public IEnumerable<string> GetIdProperties(EntityTypeBase entityType) { return from keyMember in

Pass Property to the Method in C#

醉酒当歌 提交于 2019-12-06 11:35:15
I need to pass selection of properties of some types(one type each time), assume this is my type: 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; } public long CategoryId { get; set; } [BoolAsRadio()] public bool IsScanAllowed { get; set; } } So I need a way to

Equality for .NET PropertyInfos

房东的猫 提交于 2019-12-06 00:04:53
问题 I have some code that compares 2 PropertyInfos with Equals(). While this normally seems to work, I have run into a strange situation where two reflected property info objects for the same underlying property are not equal: PropertyInfo prop1, prop2; // both are public and not static Console.WriteLine(prop1 == prop2); // false ??? Console.WriteLine(Equals(prop1, prop2)); // false ??? Console.WriteLine(prop1.DeclaringType == prop2.DeclaringType); // true Console.WriteLine(prop1.ReturnType ==

Is there a way to get entity id-field's name by reflection or whatever?

佐手、 提交于 2019-12-04 18:54:15
I am trying to get the ID field name (property name) of an entity, is it possible? User user= new User(); //User is an Entity string idField = ??????? //user.UserId If you can get the EntitySet, or the EntityType, for the entity, then you can use the KeyMembers property: public IEnumerable<string> GetIdProperties(EntitySetBase entitySet) { return GetIdProperties(entitySet.ElementType); } public IEnumerable<string> GetIdProperties(EntityTypeBase entityType) { return from keyMember in entityType.KeyMembers select keyMember.Name } You can obtain a generic object set from the context: public

Equality for .NET PropertyInfos

杀马特。学长 韩版系。学妹 提交于 2019-12-04 05:11:38
I have some code that compares 2 PropertyInfos with Equals(). While this normally seems to work, I have run into a strange situation where two reflected property info objects for the same underlying property are not equal: PropertyInfo prop1, prop2; // both are public and not static Console.WriteLine(prop1 == prop2); // false ??? Console.WriteLine(Equals(prop1, prop2)); // false ??? Console.WriteLine(prop1.DeclaringType == prop2.DeclaringType); // true Console.WriteLine(prop1.ReturnType == prop2.ReturnType); // true Console.WriteLine(prop1.Name == prop2.Name); // true Console.WriteLine(prop1

Create new PropertyInfo object on the fly

两盒软妹~` 提交于 2019-12-03 13:34:53
问题 This is my very first post, and although I have searched in topics related to my issue to some extent, I'm having a lot of trouble finding the proper answer. My question may be very simple, but I'm aware that the answer might not be so easy to give. If any exists at all. With that being said, this is my case: as an example, I have an array of PropertyInfo objects, which I am using to get the properties from a class, like this: public PropertyInfo[] GetProperties (object o) { PropertyInfo[]

Get the 'Nullable' state programmatically of a Property in Entity Framework

与世无争的帅哥 提交于 2019-12-02 14:26:38
问题 I need to get the Nullable property out for a field in EF. Some magic code needs to be done on properties that are Nullable=True, and I cannot find a working solution to get the property out. 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() ==