Click here to Skip to main content
15,904,339 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, my method must perform next action:
I need to provide additional module with types from Xml node for further hosting in WCF in windows enviroment(Console, WPF).

C#
public Dictionary<Type, Type> GetServicesToBeHostedFromConfig(XElement configuration)
		{
			try
			{
				_Locker.EnterReadLock();
				Dictionary<Type, Type> returnTypes = new Dictionary<Type, Type>();

				foreach (var item in configuration.Descendants("Items"))
				{
		                  returnTypes[Type.GetType(item.Attribute("service").Value)] = Type.GetType(item.Attribute("contract").Value);
				}

				return returnTypes;
			}
			finally
			{
				_Locker.ExitReadLock();
			}
		}


everything work fine until i try to get types which located in the same assembly , where my method located ,
but when i try to get types which containt in another i get fail! (my main assembly referenced to it !)

I suppose i must to implement some assembly resolving mechanism , but if be honest, i dont know how to do it...

Maybe someone can help me with such issue?
Posted
Comments
Sergey Alexandrovich Kryukov 20-Aug-12 17:13pm    
You are probably right, but "I get fail" is not informative. Please, exact exception information; first of all, in what line of code?
On a second thought, location and using of the types/members form the calling assembly of an assembly loaded in the same process or Application domain has never been a problem, without any resolutions. So, this is some different problem. We need more accurate information from you to be able to help...
--SA
Oleksandr Kulchytskyi 21-Aug-12 3:00am    
returnTypes[Type.GetType(item.Attribute("service").Value)] = Type.GetType(item.Attribute("contract").Value);

1 solution

First of all, let me try to convince you that getting some type or members from an assembly using its string representation is bad thing. The question is: where can you get those names? If such names come from code, you don't really need them, as this is not the only style Reflection can use (yes, this is used -- in serialization, for example). But when a string representation of a name is really needed, it comes from data where it can be misspelled. And the compiler won't give you a single warning. So, such techniques are very unreliable, first of all, unacceptable from the maintenance perspective.

So, I simply hope that I can explain you the right idea, you can change you whole approach, and then you might not need to solve your problem again. (Or you will ask a different question(s) later.)

The the right approach is to recognize certain types declared in a dynamically loaded assembly by interfaces they implement. This way, you fully avoid dependency on any names. You can declare some interfaces in an assembly used by both "host" and dynamically loaded assemblies (let me call then "plug-in assemblies", and "host" is the one loading other assemblies dynamically) in separate assembly referenced by both host and plug-in assemblies, but you can simply declare those plug-in interfaces in a host assembly and reference a host assembly by all plug-in assemblies. If you think about it, you will see that it does not create any circular references, because referential dependencies between assemblies are static and has nothing to do with run-time dependencies.

You can recognize if a type of a plug-in assembly implements some interface using System.Type.GetInterfaces:
http://msdn.microsoft.com/en-us/library/system.type.getinterfaces.aspx[^].

Are you getting the idea?

Please see my past answers to related questions:
Create WPF Application that uses Reloadable Plugins...[^],
AppDomain refuses to load an assembly[^],
code generating using CodeDom[^],
Create WPF Application that uses Reloadable Plugins...[^],
Dynamically Load User Controls[^],
C# Reflection InvokeMember on existing instance[^],
Projects and DLL's: How to keep them managable?[^].

—SA
 
Share this answer
 
Comments
Oleksandr Kulchytskyi 21-Aug-12 3:00am    
As usual =) Your answers are perfect !)
Thanks a lot for you help. For now i have clear imagination about my issue and i know how to handle it. Again , thanks)
Sergey Alexandrovich Kryukov 21-Aug-12 3:03am    
You are very welcome.
Good luck, call again.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900