This is a C# VS 2008 question...
Our system has 2 base classes, SingleEntity and NewPluralEntity . SingleEntity provides access to properties and methods related to manipulating data in a database table and NewPluralEntity is a generic base class that I inherit from to create strongly typed collections of single entities.
Here is the declaration of our NewPluralEntity class:
Here is a sample declaration of one of our inherited plural classes:
My problem is that I have to use reflection to return an ArrayList (or some other type of collection that I may enumerate) that contains my Workplaces and I cannot find any way to get the object that is returned from reflection (PropertyInfo.G etValue()) to cast to an ArrayList (or other suitable type).
For example, I have an Insured object. The Insured object has a Workplaces property. I need to use reflection to return an ArrayList that contains my Workplaces.
Here is the code for my "GetNewPluralEn tityPropertyUsi ngReflection" method:
When I invoke Insured.GetNewP luralEntityProp ertyUsingReflec tion("Workplace s"); and step into the code and place a breakpoint on my "return (ArrayList)res; " statement I can see that the "res" variable has a value and that its base type is NewPluralEntity <Workplace>, with a count and several other properties that are in NewPluralEntity , but I cannot get the cast to ArrayList to succeed.
Originally, I wanted the GetNewPluralEnt ityPropertyUsin gReflection() method to return a NewPluralEntity <Workplace> but I found no way to cast an object to a generic type when I did not know the type at compile time (in my case the type is just a string "Workplaces "). If someone knows how to do return NewPluralEntity <Workplace> in this case that would be great too but ArrayList will do.
Thanks in advance for any ideas.
Our system has 2 base classes, SingleEntity and NewPluralEntity . SingleEntity provides access to properties and methods related to manipulating data in a database table and NewPluralEntity is a generic base class that I inherit from to create strongly typed collections of single entities.
Here is the declaration of our NewPluralEntity class:
Code:
public abstract class NewPluralEntity<T> : ICollection<T>, IEnumerable<T>
Code:
public class Workplaces : NewPluralEntity<Workplace>
For example, I have an Insured object. The Insured object has a Workplaces property. I need to use reflection to return an ArrayList that contains my Workplaces.
Here is the code for my "GetNewPluralEn tityPropertyUsi ngReflection" method:
Code:
public ArrayList GetNewPluralEntityPropertyUsingReflection(string p_propertyName)
{
PropertyInfo myProperty = this.GetType().GetProperty(p_propertyName);
object res = myProperty.GetValue(this, null); //this is the fully loaded Insured object in this case
return (ArrayList)res; //blows up...
}
Originally, I wanted the GetNewPluralEnt ityPropertyUsin gReflection() method to return a NewPluralEntity <Workplace> but I found no way to cast an object to a generic type when I did not know the type at compile time (in my case the type is just a string "Workplaces "). If someone knows how to do return NewPluralEntity <Workplace> in this case that would be great too but ArrayList will do.
Thanks in advance for any ideas.
Comment