Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Dynamically loaded assemblies. Reflection & Dynamic Types

Posted on 2010-08-18
5
Medium Priority
?
676 Views
Last Modified: 2013-11-12
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(Type.EmptyTypes);
            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
0
Comment
Question by:Jobbietronic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 16

Expert Comment

by:kris_per
ID: 33465985
One option is to use interface. If all the custom types (Car, Book, etc)implement an interface (say IBuildable), then you can cast your returnObject like:

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

Author Comment

by:Jobbietronic
ID: 33466222
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?
0
 
LVL 16

Accepted Solution

by:
kris_per earned 2000 total points
ID: 33466383

Yes. Both the projects needs to know about the interface.

Another method using reflection is:

Even if you get the type of 'returnObject' and create an object for that type (or anything), it will be again an object and it can not be used like a declared variable like List<Car> as business project doesn't know the types;

So I instead of trying to cast the list to the List<...>, you can use reflection on the objects in that list to get values/call methods as shown in the code below (with a linq query). Here one thing the business project need to know is that the object in the list has a property named 'Name'....



IEnumerable<object> listE = returnObject as IEnumerable<object>;

var qry = from c in listE 
          where c.GetType().GetProperty("Name").GetValue(c,null).ToString() == "name1" 
          select c;

Open in new window

0
 

Author Comment

by:Jobbietronic
ID: 33466633
Thanks Kris, with a little rewrite of my query engine I think I can get there!
0
 

Author Closing Comment

by:Jobbietronic
ID: 33466639
Great help and quick too.
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question