propertyinfo

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

守給你的承諾、 提交于 2019-12-02 08:15:10
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() == typeof(Nullable<>); // isNullable always returns false !!!! and I need it to return true if the field is

Setting all null object parameters to string.empty

最后都变了- 提交于 2019-12-02 03:57:49
I have an object that is contains strings and further objects that contain strings, what i need to do is ensure that the object and any sub objects have an empty string and not a null value, so far this works fine: foreach (PropertyInfo prop in contact.GetType().GetProperties()) { if(prop.GetValue(contact, null) == null) { prop.SetValue(contact, string.empty); } } the problem is this only works for the objects strings and not the sub-objects strings. Is there a way to also loop over all sub-objects and set their strings to string.Empty if found to be null ? Here's an example of the 'contact'

Why is a member of base class different from the same member in derived class?

不羁岁月 提交于 2019-12-01 20:54:20
问题 This is a followup to this question: Lambda expression not returning expected MemberInfo class Human { public string name { get; set; } } class Man : Human { } var m1 = typeof(Human).GetProperty("name"); var m2 = typeof(Man).GetProperty("name"); //m1 != m2 why? The same holds for MethodInfo s. I can understand there has to be a difference when Human is an interface, or when name of Human is abstract/virtual. But why is it so for sealed types? Isn't name of Man exactly name of Human ?

How do you get the Value of a property from PropertyInfo?

孤者浪人 提交于 2019-12-01 02:47:15
I've got an object that has a collection of properties. When I get the specific entity I can see the field I'm looking for ( opportunityid ) and that it's Value attribute is the Guid for this opportunity. This is the value I want, but it won't always be for an opportunity, and therefore I can't always look at opportunityid , so I need to get the field based on input supplied by the user. My code so far is: Guid attrGuid = new Guid(); BusinessEntityCollection members = CrmWebService.RetrieveMultiple(query); if (members.BusinessEntities.Length > 0) { try { dynamic attr = members.BusinessEntities

How do I determine if a property was overridden?

前提是你 提交于 2019-11-30 18:08:01
I am doing a project that where I need to register all the properties, because of the system being so huge it would require a lot of work to register all the properties that i want to be dependent for the purpose of Xaml. The goal is to find all properties that are on the top of the tree. so basically public class A{ public int Property1 { get; set; } } public class B : A{ public int Property2 { get; set; } public virtual int Property3 { get; set; } } public class C : B{ public override int Property3 { get; set; } public int Property4 { get; set; } public int Property5 { get; set; } } The end

C# Developing .Net3.5 using reflection to get/set values to nested properties and/or nested fields

霸气de小男生 提交于 2019-11-29 17:14:49
I'm developing an app that works with data block classes inheritied from a base class and I am trying to use Reflection to drill down to properties/fields in my data block class. Since all the data block classes are derived/inherited from the base class (which contains a Size property) I can use a general variable of type base class to create an object in my app easily enough; I can also get/set properties at a top level. My problem occurs when the property is a field - I do not know how to get to the next level in the field to get to the base properties and/or fields, if applicable. My

How to get Getter backing field from PropertyInfo?

試著忘記壹切 提交于 2019-11-29 17:05:12
I have a class as follows: class Foo : PropertyChangedBase { private int _property; public int Property { get { return _property; } set { OnAssignPropertyChanged("Property", () => _property, value); } } PropertyChangedBase implements INotifyPropertyChanged with the following methods: protected void OnAssignmentPropertyChanged<T>(string propertyName, Expression<Func<T>> fieldExpression, T value) { var get = fieldExpression.Compile(); if (get().Equals(value)) { return; } // invoke set property method SetProperty(fieldExpression, value); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs

Setting value in an array via reflection

人盡茶涼 提交于 2019-11-29 13:40:11
Is there a way to set a single value in an array property via reflection in c#? My property is defined like this: double[] Thresholds { get; set; } For "normal" properties I use this code to set it via reflection: PropertyInfo pi = myObject.GetType().GetProperty(nameOfPropertyToSet); pi.SetValue(myObject, Convert.ChangeType(valueToSet, pi.PropertyType), null); How would I have to change this code to set the value in an array property at an arbitrary position? Thanks! BTW: I tried to use the index parameter, but that seems only to work for indexed properties, not properties that are arrays...

PropertyInfo : is the property an indexer?

*爱你&永不变心* 提交于 2019-11-29 09:02:35
I have the following code : PropertyInfo[] originalProperties = myType.GetProperties(); I want to exclude from originalProperties all the indexers (myVar["key"] appears as property named "Item"). What is the proper way ? Exclude all properties where propInfo.Name == "Item" is not an option. Call PropertyInfo.GetIndexParameters - if the returned array is empty, it's not an indexer. Another option is to use: myType.GetProperties().Except(myType.GetDefaultMembers().OfType<PropertyInfo>()); GetDefaultMembers will return all the compiler generated indexers in C#. This has the advantage of not

How to set Vaues to the Nested Property using C# Reflection.?

ぃ、小莉子 提交于 2019-11-28 18:49:28
I am trying to set a value to a Nested Property of Class dynamically using reflection. Could anyone help me to do this. I am having a class Region like below. public class Region { public int id; public string name; public Country CountryInfo; } public class Country { public int id; public string name; } I have a Oracle Data reader to provide the Values from the Ref cursor. which will give me as Id,name,Country_id,Country_name I could able to assign the values to the Region.Id, Region.Name by below. FieldName="id" prop = objItem.GetType().GetProperty(FieldName, BindingFlags.Public |