I wanted to make a dynamic method in which i pass any instance of object i made, for example:
public class Employee()
{
public string FName;
public string SName;
.....etc.
}
The method i want is like the following:
public void methodName(Object oObject)
{
//lets say that i have passed object employee to it.
}
Is it possible to extract any values from the oObject, i have tried to do so but it have no attributes to do so, and when i put in the watch i can acutally see all the attribute of class employee in it.
So is there any way ?
There are many ways.Update: you say you want the whole object. Casting or interfaces to expose the properties is the way foward.
You can cast the object to the specific type:
You can expose an interface that defines what the method expects, then provided types implement the interface:
You can use
dynamicand hope for the best at runtime (.NET 4):And then there is reflection.
But the better question to ask is, is this a good method? I would be a little skeptical about methods that take anything and make expectations about things… though that’s not to say it doesn’t or shouldn’t happen.