The MSDN documentation doesn't generally specify the assembly a namespace is in, so there's no easy way to add the necessary assembly reference.
NOTE: I am using MonoDevelop, so right-click, resolve is not an option.
Take a look at this example: http://msdn.microsoft.com/en-us/library/dd321424(VS.100).aspx
Task(Of TResult) Class
Represents an asynchronous operation
that can return a value.
Namespace: System.Threading.Tasks
Assembly: mscorlib (in mscorlib.dll)
Namespaces can cross multiple assemblies, so you need to look for the assembly a specific class is in.
Related
When I try to load an assembly using this method, what's the differences between long name and partial name?
From MSDN, I get this:
for long name: A full assembly reference is required if you reference any assembly that is part of the common language runtime or any assembly located in the global assembly cache.
for partial name: the runtime looks for the assembly only in the application directory
And a customer encountered an error because I used partial name to load the assembly, but the assembly was exactly in the application directory.
So, I want to know, are there any other differences between long name and partial name when using the Load(string) method?
Thanks a lot!
#gTiancai: On a safer side, setting the AssemblyName.CodeBase property might be helpful.
This might help: Best Practices for Assembly Loading
I downloaded a project that has a reference to Microsoft.Windows.Design.dll, but it's not on my system. Where can I get this package?
using Microsoft.Windows.Design.PropertyEditing;
That namespace is typically found in the Microsoft.Windows.Design.Interaction.dll assembly, not Microsoft.Windows.Design.dll.
For example, the NewItemFactory class:
Namespace: Microsoft.Windows.Design.PropertyEditing
Assembly: Microsoft.Windows.Design.Interaction (in Microsoft.Windows.Design.Interaction.dll)
I have 2 versions of same dlls. Say named, Test.dll. I want to call 2 dlls from my console application.
I tried to use Extern alias. But it is calling new dll. I am calling these 2 dlls from my DAL class.
Any help would be appreciated.
Thanks,
This is not the default way of how you do things in .net, therefore coding will not be easy in such a manner. As #Johnathon Reinhart says in his answer, you will have to use Assembly.Load (by passing the fully qualified assembly name to the function).
Like this:
Assembly asmOld = Assembly.Load("MyAssembl, Version=1.0.0.1, Culture=neutral, PublicKeyToken=ab1234567defabc1");
Assembly asmNew = Assembly.Load("MyAssembl, Version=2.0.0.1, Culture=neutral, PublicKeyToken=ab1234567defabc1")
Moreover, you will have to keep reference to both assemblies and then use Assembly.CreateInstance to create instances of the types you need. After that, you will have to call members using reflection (something like this). Like this:
Ojbect objOld = asmOld.CreateInstance("MyApp.Namespace.Classname");
Ojbect objNew = asmNew.CreateInstance("MyApp.Namespace.Classname");
objOld.GetType().InvokeMember("TestMethod", BindingFlags.InvokeMethod,null,obj,null);
objNew.GetType().InvokeMember("TestMethod", BindingFlags.InvokeMethod,null,obj,null);
To improve your code writing, you could use the LateCall from Microsoft.VisualBasic.CompilerServices to work with your objects. There is a nice wrapper for that by Andy Adinborough - http://andy.edinborough.org/Use-Late-Binding-in-C-Now-without-NET-4-0
I'm assuming that these DLLs are .NET assemblies, and not just standard C DLLs.
If so, I think you can specifically load the assembly with the static Assembly.LoadFrom(string assemblyFile). Then I think you can get a module from that assembly with Assembly.GetModule().
you can use Assembly.LoadFile or using alias
Loading Multiple Versions of same Assembly
Using different versions of the same dll in one application
I can add the statement using System.Configuration; to my code using intellisense, but I can't get any intellisense when I access the type ConfigurationManager.
Why does intellisense work when entering the using directive, but doesn't work when specifying a type from that namespace?
using System.Configuration;
namespace TestDBMSConnection
{
class Program
{
static void Main(string[] args)
{
var dataProviderName = ConfigurationManager.AppSettings["provider"];
}
}
}
I can fix the issue by adding a reference to System.Configuration, but I don't understand why I don't need to do that for intellisense to work for the using directive.
Classes in a namespace are not required to be located in the same assembly.
Some classes in the System.Configuration namespace are located in System.dll (such as SettingsBase) while some other classes are located in System.Configuration.dll (such as ConfigurationManager).
IntelliSense can only suggest classes and namespaces in referenced assemblies. So if you have System.dll referenced but not System.Configuration.dll, IntelliSense can suggest the System.Configuration namespace and the System.Configuration classes located in System.dll, but not those located in System.Configuration.dll.
IntelliSense can also not know in which unreferenced assembly a certain class might be located. So you have to reference System.Configuration.dll manually before you can use it.
Because intellisense forusing shows you the list of known namespaces. And System.Configuration namespace is already referenced(Some of the classes in this namespace are in system.dll or mscorlib.dll), while the ConfigurationManager class is in System.Configuration.dll which is not referenced, and thus intellisense does not know about it.
How do I obtain an instance of a Type class for a type not defined in the currently executing assembly or in mscorlib.dll?
a) Namely, I've defined a class type someType in assembly CSharpSnapIn.dll, located at E:\CSharpSnapIn.dll, but for some reason when I try to specify an absolute path to this assembly, I get an exception:
Type t = Type.GetType("someType, E:\\CSharpSnapIn.dll"); // exeception
b) I've also tried by putting CSharpSnapIn.dll into \bin\debug directory of a currently running application, but I still get an exception:
Type t = Type.GetType("someType, CSharpSnapIn.dll"); // exeception
thanx
EDIT:
1) I've declared another class type someType2 ( inside CsharpSnapIn.dll )and this time it worked:
Type.GetType("someType2, CSharpSnapIn");
Difference between someType and someType2 is that someType implements an interface declared in external assembly asmIn, but this shouldn't cause an exception, since CsharpSnapIn.dll does have a reference to asmIn?!
2)
Note that the assembly doesn't need to
be loaded first, so long as the
assembly resolver can find it
In other words, calling Type.GetType() first loads an assembly and then creates a Type instance?
3)
The assembly has to be found by
probing, so it would have to be in the
bin directory as per your second
example. If it's an assembly with a
strong name, you have to give all the
details.
So you're saying we can't specify an absolute path ( to an assembly ) using Type.GetType(), but instead assembly needs to reside inside a bin directory?
You will need to first load the assembly:
Type t = Assembly
.LoadFrom(#"e:\CSharpSnapIn.dll")
.GetType("SomeNs.SomeType", true);
You need to give the assembly name - not the file which contains it.
For example:
Type t = Type.GetType("someType, CSharpSnapIn");
The assembly has to be found by probing, so it would have to be in the bin directory as per your second example. If it's an assembly with a strong name, you have to give all the details. Note that someType here also has to be fully qualified in terms of the namespace.
Note that the assembly doesn't need to be loaded first, so long as the assembly resolver can find it. For example, if the assembly is in the same directory as the currently executing assembly, that will be fine in most cases.
As Darin says, an alternative is to load the assembly directly - although in my experience there are quite a few "gotchas" in loading assemblies explicitly, particularly if you've got two assemblies in different locations which both rely on a third assembly. Making sure you only get that third assembly loaded once can be tricky.
You need to choose between LoadFile and LoadFrom etc. Here are some remarks from MSDN:
Use the LoadFile method to load and
examine assemblies that have the same
identity, but are located in different
paths. LoadFile does not load files
into the LoadFrom context, and does
not resolve dependencies using the
load path, as the LoadFrom method
does. LoadFile is useful in this
limited scenario because LoadFrom
cannot be used to load assemblies that
have the same identities but different
paths; it will load only the first
such assembly.