How to get instantiated object from a string
-
I need to use Reflection to retrieve the properties of a given object. That's easy. The problem is that I don't know which object to use until run-time. The object is already available and I can't use Reflection to re-create it since its constructor is not available. Let me clarify with some code:
object obj = GetInstantiatedObject(objectName);
Type type = obj.GetType();
foreach (PropertyInfo property in type.GetProperties())
{
....
}Does anyone know what
object GetInstantiatedObject(string objectName)
should contain? Thanks in advance! Al- Is God willing to prevent evil, but not able? Then he is impotent. - Is he able, but not willing? Then he is malevolent. - Is he both able and willing? Whence then is evil? - Is he neither able nor willing? Then why call him God? Epicurus
-
I need to use Reflection to retrieve the properties of a given object. That's easy. The problem is that I don't know which object to use until run-time. The object is already available and I can't use Reflection to re-create it since its constructor is not available. Let me clarify with some code:
object obj = GetInstantiatedObject(objectName);
Type type = obj.GetType();
foreach (PropertyInfo property in type.GetProperties())
{
....
}Does anyone know what
object GetInstantiatedObject(string objectName)
should contain? Thanks in advance! Al- Is God willing to prevent evil, but not able? Then he is impotent. - Is he able, but not willing? Then he is malevolent. - Is he both able and willing? Whence then is evil? - Is he neither able nor willing? Then why call him God? Epicurus
Al Beback wrote:
The object is already available and I can't use Reflection to re-create it since its constructor is not available.
I don't really understand that part - you already have an object, why not just use it?
Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro
-
Al Beback wrote:
The object is already available and I can't use Reflection to re-create it since its constructor is not available.
I don't really understand that part - you already have an object, why not just use it?
Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro
S. Senthil Kumar wrote:
I don't really understand that part - you already have an object, why not just use it?
Because it's not just one. Let's say that it can be up to 10 (or more), and I don't know which one I will be using until the program is run. So I can do this: object obj; if (name == "Object1") obj = Object1; else if (name == "Object2) obj = Object2; etc... Then if more objects come into the picture, I have to modify this code. I'm wondering if there's a way to use reflection to avoid doing all of that. Thanks, Al
- Is God willing to prevent evil, but not able? Then he is impotent. - Is he able, but not willing? Then he is malevolent. - Is he both able and willing? Whence then is evil? - Is he neither able nor willing? Then why call him God? Epicurus