Jobbietronic
asked on
Dynamically loaded assemblies. Reflection & Dynamic Types
In my silverlight project when i user drops a file on the page, based on the file name I load another assembly dynamically to handle data it contains.
In the dynamic assembly I have a class e.g. book and a method which returns a List<book>, if the user drops another file type i may dynamically download an assembly that deals with cars; it will have the class car and a method which returns List<car>.
The point is that i don't want my business model project to know anything about the contents of these dynamic assemblies other than the fact that they have a 'GetData' method which will return a List<> of whatever items they deal with i.e. books, cars....
The code in my business model project is below. I need a way (I presume using reflection) to convert the returnObject from a plain old object to a List<> so that I can make use of it effectively in my business model project.
public object BuildDataObject(bool Merge, string FileType, object Data, Assembly DownloadedAssembly, string _Project)
{
object[] _params = new object[3];
_params[0] = Data;
_params[1] = Merge;
_params[2] = false;
Type _Object = _Assembly.GetType(_Project + ".Get");
MethodInfo mi = _Object.GetMethod("GetData ");
ConstructorInfo ci = _Object.GetConstructor(Typ e.EmptyTyp es);
object response = ci.Invoke(null);
object returnObject = mi.Invoke(response, _params);
return returnObject;
}
//returnObject is a List<myCustomItem>
so using reflection i can find out what the originating Type of the object was like...
Type originalType = returnObject.GetType();
...but I'm really struggling with how to cast 'returnObject' back to List<myCustomItems> so that I can use Linq on it.
Silverlight 4, C#4
In the dynamic assembly I have a class e.g. book and a method which returns a List<book>, if the user drops another file type i may dynamically download an assembly that deals with cars; it will have the class car and a method which returns List<car>.
The point is that i don't want my business model project to know anything about the contents of these dynamic assemblies other than the fact that they have a 'GetData' method which will return a List<> of whatever items they deal with i.e. books, cars....
The code in my business model project is below. I need a way (I presume using reflection) to convert the returnObject from a plain old object to a List<> so that I can make use of it effectively in my business model project.
public object BuildDataObject(bool Merge, string FileType, object Data, Assembly DownloadedAssembly, string _Project)
{
object[] _params = new object[3];
_params[0] = Data;
_params[1] = Merge;
_params[2] = false;
Type _Object = _Assembly.GetType(_Project
MethodInfo mi = _Object.GetMethod("GetData
ConstructorInfo ci = _Object.GetConstructor(Typ
object response = ci.Invoke(null);
object returnObject = mi.Invoke(response, _params);
return returnObject;
}
//returnObject is a List<myCustomItem>
so using reflection i can find out what the originating Type of the object was like...
Type originalType = returnObject.GetType();
...but I'm really struggling with how to cast 'returnObject' back to List<myCustomItems> so that I can use Linq on it.
Silverlight 4, C#4
ASKER
Kris,
thanks for the reply, I'm sure the above is an eminently sensible solution but my inexperience dictates that I'll have to spend some time looking into how to implement it!
I have an initial question:
Would that mean that both my business model project and the dynamic plugin projects would need to know about IBuildable at compile time?
thanks for the reply, I'm sure the above is an eminently sensible solution but my inexperience dictates that I'll have to spend some time looking into how to implement it!
I have an initial question:
Would that mean that both my business model project and the dynamic plugin projects would need to know about IBuildable at compile time?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Kris, with a little rewrite of my query engine I think I can get there!
ASKER
Great help and quick too.
IEnumerable<IBuildable> list = returnObject as IEnumerable<IBuildable>;
Now you can use this list in Linq and use the common properties/methods defined in the IBuildable interface in the linq query...