Click here to Skip to main content
15,893,644 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am developing a console application abc.exe. I have another assembly called abc.xyz.dll. Both the applications are in same solution

Structure
abc
--abc
----abc.exe
--abc.xyz
----abc.xyz.dll

I want to dynamically load the abc.xyz.exe assembly in the console application abc.exe where the name of assembly is mentioned in app.config file
XML
<appSettings>
    <add key="modelType" value="abc.xyz.Worker,abc.xyz" />    
  </appSettings>



I have tried
C#
string assembName = ConfigurationManager.AppSettings.Get("modelType");

C#
type = Type.GetType(assembName);


but it returns null. I do not want to load the assembly in GAC or specify the path in environment variable.

Please help me with a solution.


I am trying to solve below problem
Develop a console application which would probe a given model class at runtime, and ask the user to fill the object.

The model class, as part of our assignment, is a simple class as shown below:

C#
[Model(DisplayName = "Employee")]
public class Worker
{
[Display(Name = "Employee ID")]
public int WorkerId { get; set; }
[Display(Name = "Full Name")]
public string Name { get; set; }
[Display(Name = "Date of Birth")]
public DateTime DateOfBirth { get; set; }
[Display(Name = "Salary")]
public decimal Remuneration { get; set; }
}


Create an assembly named ‘DynamicConsole.Core.dll’, and create an attribute ‘Model’ with ‘DisplayName’ property.

o In Visual Studio, use code snippet ‘attribute’ for the recommended pattern.
 Create another assembly named ‘Organization.Domain.dll’, and create model class Worker as shown above.

o You would need to add references to ‘DynamicConsole.Core.dll’ and ‘System.ComponentModel.DataAnnotations.dll’ to resolve references of ‘Model’ and ‘Display’ attributes.

 Create a console application ‘DynamicConsole.exe’.
o This application will pick the model type from app.config, and ask the user to fill in the required details.

(key="modelType", value="FullyResolvedTypeName, AssemblyName")

o You would need to add references to ‘DynamicConsole.Core.dll’ and ‘System.ComponentModel.DataAnnotations.dll’ to resolve references of ‘Model’ and ‘Display’ attributes.

o Don’t add reference to ‘Organization.Domain.dll’ as it should be picked from
app.config during runtime.

<appsettings>
<add key="modelType" value="Organization.Domain.Worker, Organization.Domain">
Posted
Updated 2-Dec-15 23:07pm
v9
Comments
George Jonsson 3-Dec-15 2:40am    
Do you want to load an assembly as a dll or do you want to start a new process?
Usually you don't load exe-files.
AnkitaS12 3-Dec-15 3:19am    
George, I want to load the dll ( i am sorry for the mistake in the question for writing exe) I have updated the question

You already got an answer, but I'm not sure that you confusion is dismissed. From the code you attempted, it's clear that 1) you did not even try to load an assembly (but you already learned about it later); 2) you misunderstand assembly name and the function of the type.

No problem; it's easy to be confused with this naming. You just need to take a closer look at MSDN. Your assemblyName is not an assembly name. And assembly name is not a string at all. System.Reflection.AssemblyName is a more complicated data structure, it can identify the assembly uniquely, which is important for GAC, and carry other important function: https://msdn.microsoft.com/en-us/library/system.reflection.assemblyname%28v=vs.110%29.aspx[^].

If you load assembly using its name, in practice, it's only from GAC. You probably have some file name in your config file. Here is another thing you need to understand: the file is not the assembly, even when it is the only artifact representing an assembly. The file is always a module, and a module may or may not have assembly manifest. By default, Visual Studio always create single-module assemblies, but on lower level you can always create an assembly made of more than 1 module; in this case, only one of these modules will have an assembly manifest. And its file name is the name you have to use in, say, Assembly.LoadFrom. It has nothing to do with assembly name.

See also:
https://msdn.microsoft.com/en-us/library/7d3c18c2%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.reflection.assembly.getname%28v=vs.110%29.aspx[^].

Another confusion is: you need to understand System.Type. First of all, System.Type.GetType(string) expects type name, which has nothing to do with assembly name and nothing to do with a module file name. It can only be used on some assembly types when an assembly is already loaded: https://msdn.microsoft.com/en-us/library/w3f99sx1(v=vs.110).aspx[^].

And this is a worst thing one can possibly do. With dynamic loading of assemblies, you are building something like a plug-in system, where a dynamically loaded assembly is a kind of plug-in. But how to use the loaded assembly? The problem with GetType is: and what is the type under a given name is absent? Worse, what if it is misspelled in the host application?

No, this is not well maintainable. At the same time, there is at least one rock-solid approach. You could define some interface known to both host and dynamically loaded assembly, implement it by some of the types in the dynamically loaded assembly — that will it easily recognizable without using any strings. You can do some steps to make it very efficient mechanism. Please see my past answers for further detail:
Dynamically Load User Controls[^],
C# Reflection InvokeMember on existing instance[^],
Gathering types from assemblies by it's string representation[^],
What is the use of plugin in general[^],
adding reference programmatically[^],
a summary referencing other answers can be found here: Access a custom object that resides in plug in dll[^].

—SA
 
Share this answer
 
Comments
George Jonsson 3-Dec-15 18:35pm    
Good explanation.
Sergey Alexandrovich Kryukov 3-Dec-15 19:19pm    
Thank you, George.
—SA
AnkitaS12 3-Dec-15 23:29pm    
Thank You Sergey for the detailed explanation. I am still trying to grasp these terms and concepts and they end up confusing me even more. But sure your explanation helped.
Sergey Alexandrovich Kryukov 4-Dec-15 0:29am    
You are welcome. I understand. You can always ask some further questions...
Are you going to accept this answer formally?
—SA
Refer to this MSDN page How to: Load Assemblies into an Application Domain[^]

[UPDATE]
You can also use Load and the long name of the assembly, see Assembly.Load Method (String)[^]
 
Share this answer
 
v3
Comments
AnkitaS12 3-Dec-15 3:56am    
If i use LoadFrom() , how will i specify the relative path. can you help me with that
George Jonsson 3-Dec-15 4:00am    
See my updated answer
AnkitaS12 3-Dec-15 4:27am    
But this wont work if we have to fetch the assembly name from the app.config as mentioned in original question. I need to fetch the name from app.config and then load that assembly.
George Jonsson 3-Dec-15 4:35am    
Can't you change the contents of the app config?
AnkitaS12 3-Dec-15 4:36am    
No this is a particular requirement for one of my assignment

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