propertyinfo

PropertyInfo.GetValue() "Object does not match target type."

I'm digging into Reflection for the first time and I'm truely stuck. I've googled everything I can think of. I'm 90% where I wanna be now. I'm trying to return the value of a Property in a custom class through Reflection. Here's my class declaration: Public Class Class2 Private newPropertyValue2 As String Public Property NewP...

Finding the hosting PropertyInfo from the MethodInfo of getter/setter

Hi, 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? ...

c# How do you get the base.Name from PropertyInfo

I am stepping though some code and looking at a PropertyInfo object and want to know how to get its base.Name I can see this in the debugger but I am not sure how to do this as there is no "base" property on a PropertyInfo ...

Get the default PropertyDescriptors for a type

I'm customizing how an object type is displayed in a PropertyGrid by implementing ICustomTypeDescriptor. I'm allowing the user to create their own custom properties that are stored in a single dictionary of keys and values. I'm able to create all the PropertyDescriptors for these values and view them in the property grid. However, I also...

Reflection - getting properties of nested objects

refers to : http://stackoverflow.com/questions/906327/reflection-setting-type-of-returned-obj I have a object Call Jobcard with a few properties, one of which is another object called Customer with its own properties, one of which is another nested object called Adress. These 2 functions will be handling other object types as well. pr...

PropertyInfo Sub Properties

I have a linq Context that I am looking at all the data Tables, I am trying to get the list of fields in all tables foreach (var code in ctx.GetType().GetProperties()) { Console.WriteLine(code.PropertyType + " - " + code.Name + " "); if (code.PropertyType.ToString().Contains("System.Data.Linq.Tab...

Reflecting on property to get attributes. How to do when they are defined elsewhere?

I have a class Bar like this: class Foo : IFoo { [Range(0,255)] public int? FooProp {get; set} } class Bar : IFoo { private Foo foo = new Foo(); public int? FooProp { get { return foo.FooProp; } set { foo.FooProp= value; } } } I need to find the attribute [Range(0,255)] reflecting ONLY on the property ...

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

Hi 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 ...

C# - setting a property by reflection with a string value

Hi, I'd like to set a property of an object through reflection, with a value of type string. So, for instance, suppose I have a Ship class, with a property of Latitude, which is a double. Here's what I'd like to do: Ship ship = new Ship(); string value = "5.5"; PropertyInfo propertyInfo = ship.GetType().GetProperty("Latitude"); propert...

How to tell is a PropertyInfo is of a particular enum type?

I have the following code; public class DataReader<T> where T : class { public T getEntityFromReader(IDataReader reader, IDictionary<string, string> FieldMappings) { T entity = Activator.CreateInstance<T>(); Type entityType = entity.GetType(); PropertyInfo[] pi = entityType.Get...

It throws an stackoverflow exception when I user PropertyInfo.SetValue()

When I use PropertyInfo.SetValue in asp.net , it throws a stackoverflow exception. That I write this code: for (int i = 0; i < rivalSeriesIDList.Count; i++) { cardb_series rivalSeries = seriesBll.GetSeriesInfoByID(rivalSeriesIDList[i].ToString()); this.GetType().GetProperty("brandid" + (i + 1)).SetValue(this, riv...

How would I know if a property is a generic collection

I need to know if the type of a property in a class is a generic collection (List, ObservableCollection) using the PropertyInfo class. enter code here foreach (PropertyInfo p in (o.GetType()).GetProperties()) { if(p is Collection<T> ????? ) } ...

Control Property

Can anyone help me. I have an application it contain many aspx pages. here i am collecting all controls id's(ex:label1,texbox1,grid1.....etc ) from all page in the application till its working fine.... now i want to collect text property of that contros(ex: label1 have text property StudentId ...etc) how i can do it.... here i have cod...

Can I set a property on an object that is only declared on the instance type, when I don't know the type?

Let me explain. I have a List into which I am adding various ASP.NET controls. I then wish to loop through the list and set a CssClass, however not every Control supports the property CssClass. What I would like to do is test if the underlying instance type supports the CssClass property and set it, but I'm not sure how to do the conver...

Finding the name of a particular property of a class from within the class in C# 3.5

Give a class like below, how can i find the name of one particluar property? public class Student { public int Grade { get; set; } public string TheNameOfTheGradeProperty { get { return ???? } } // More properties.. } So i would like to return the st...

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

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); ...

Why is TargetInvocationException treated as uncaught by the IDE?

I have some code that is using reflection to pull property values from an object. In some cases the properties may throw exceptions, because they have null references etc. object result; try { result = propertyInfo.GetValue(target, null); ...

Ignore collection properties in PropertyInfo

I have a function with this code: foreach (PropertyInfo propertyInfo in typeof(T).GetProperties()){ //SOME CODE if (propertyInfo.CanWrite) propertyInfo.SetValue(myCopy, propertyInfo.GetValue(obj, null), null); } I would avoid to check "collection" properties; to do this now I have insert this control: if (propertyInfo.PropertyTy...

Members of object not being returned by PropertyInfo c#

I'm trying to use PropertyInfo interate through a class and create a datatable from it. However it returns no values. I'm a little stumped; public class thetransactions { public string FirstName; public string Surname; public string PreviousOwner; public string NewOwner; public string postcode; public string[] ...

Reflection PropertyInfo.GetValue from Collection

I have problem with reflection, dynamic invoking objects and reading collection values. In Referenced COM/Interop it would look like this: ICollection collection = (ICollection)sth.getCollection("parameter"); SomeObject obj = (SomeObject)collection["id='1'"]; //DB WHERE condition Unfortunetly i need to make it with reflection and dyna...