propertyinfo

How do I determine if a property was overridden?

試著忘記壹切 提交于 2019-12-18 19:39:13
问题 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

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

六月ゝ 毕业季﹏ 提交于 2019-12-18 09:37:04
问题 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

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

时间秒杀一切 提交于 2019-12-17 22:23:38
问题 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

Extension method to get property name

喜夏-厌秋 提交于 2019-12-17 20:51:20
问题 I have an extension method to get property name as public static string Name<T>(this Expression<Func<T>> expression) { MemberExpression body = (MemberExpression)expression.Body; return body.Member.Name; } I am calling it as string Name = ((Expression<Func<DateTime>>)(() => this.PublishDateTime)).Name(); This is working fine and returns me PublishDateTime as string. However I have an issue with the calling statement, it is looking too complex and I want some thing like this. this

Finding the hosting PropertyInfo from the MethodInfo of getter/setter

无人久伴 提交于 2019-12-17 16:15:25
问题 I do some type analysis in runtime using Reflection. If I have a MethodInfo instance, how can I figure out if this is a "real" method or is a getter/setter method of a property? And if it is a property, how can I find the its hosting PropertyInfo back? 回答1: Ecma 335 specifies (but does not demand) that compilers use the get_/set_ prefixes (chapter 22.28). I don't know any language that breaks that recommendation. Making it easy: public static PropertyInfo GetPropFromMethod(Type t, MethodInfo

Is there a way to set properties on struct instances using reflection?

时光毁灭记忆、已成空白 提交于 2019-12-17 02:38:16
问题 I'm trying to write some code that sets a property on a struct (important that it's a property on a struct) and it's failing: System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(); PropertyInfo propertyInfo = typeof(System.Drawing.Rectangle).GetProperty("Height"); propertyInfo.SetValue(rectangle, 5, null); The Height value (as reported by the debugger) never gets set to anything - it stays at the default value of 0. I have done plenty of reflection on classes before and this has

Property SetValue returns TargetParameterCountException

旧街凉风 提交于 2019-12-13 04:06:12
问题 TL;DR I have the class Type for a XmlSerialize r which throws a TargetParameterCountException on prop.SetValue() . Class Type The class is called from my List Types [System.Xml.Serialization.XmlArrayItemAttribute("Type", IsNullable = false)] public List<Type> Types { get; set; } and the class itself [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] public partial class Type { [System.Xml

How to get property name and value from generic model with generic list?

僤鯓⒐⒋嵵緔 提交于 2019-12-11 14:57:15
问题 Using the following model as an example. public class FooModel { public FooModel() { Bars= new List<BarModel>(); } [ManyToMany] public IList<BarModel> Bars{ get; set; } } public class BarModel { public int Id { get; set; } } I need to extrapolate the List<BarModel> from a fooModel object, and build up a Dictionary<string, object> from each BarModel in the list. Let's say I create the following object. var fooModel = new FooModel(); var bar1 = new BarModel {Id = 1}; var bar2 = new BarModel {Id

VB.NET: Instantiate a nested property by reflection

别说谁变了你拦得住时间么 提交于 2019-12-11 08:06:23
问题 I want to set the values of the properties via reflection. In this thread they propose a solution. But the problem with the solution is that it is not instantiating the properties. But I want to check and instantiate the properties if necessary. My DTO is: Public Class root Public Property Printing() As rootPrinting End Class Public Class rootPrinting Public Property Printer() As String Public Property PrinterBatch() As String End Class Now for setting the properties I have defined the

How to get Getter backing field from PropertyInfo?

心不动则不痛 提交于 2019-12-11 06:58:16
问题 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