Export (0) Print
Expand All

Assembly.LoadFrom Method (String)

.NET Framework 1.1

Loads an assembly given its file name or path.

[Visual Basic]
Overloads Public Shared Function LoadFrom( _
   ByVal assemblyFile As String _
) As Assembly
[C#]
public static Assembly LoadFrom(
 string assemblyFile
);
[C++]
public: static Assembly* LoadFrom(
 String* assemblyFile
);
[JScript]
public static function LoadFrom(
   assemblyFile : String
) : Assembly;

Parameters

assemblyFile
The name or path of the file that contains the manifest of the assembly.

Return Value

The loaded assembly.

Exceptions

Exception Type Condition
ArgumentNullException assemblyFile is a null reference (Nothing in Visual Basic).
FileNotFoundException assemblyFile is not found, or the module you are trying to load does not specify a filename extension.
BadImageFormatException assemblyFile is not a valid assembly.
SecurityException A codebase that does not start with "file://" was specified without the required WebPermission.
PathTooLongException An assembly or module was loaded twice with two different evidences, or the assembly name is longer than MAX_PATH characters.
ArgumentException The assemblyFile parameter is the empty string ("").

Remarks

The assemblyFile parameter must refer to a URI without escape characters. This method supplies escape characters for all invalid characters in the URI.

assemblyFile is relative to the current directory, and the assembly is loaded into the domain of the caller.

The LoadFrom methods use a load context that records the assembly name and the assembly instance information for the set of assemblies that is the transitive closure of the assemblies loaded by the application using LoadFrom. The LoadFrom load context applies to assemblies that are loaded using their locations.

Whether certain permissions are granted or not granted to an assembly is based on evidence. The rules for assembly and security evidence merging are as follows:

  • When you use a LoadFrom method with no Evidence parameter, the assembly is loaded with the evidence that the loader supplies.
  • When you use a LoadFrom method with an Evidence parameter, pieces of evidence are merged. Pieces of evidence supplied as an argument to the LoadFrom method supersede pieces of evidence supplied by the loader.
  • When you use a LoadFrom method with a Byte[] parameter to load a common object file format (COFF) image, evidence is combined. Zone, Url and Site are inherited from the calling assembly, and Hash and StrongName are taken from the COFF assembly.
  • When you use a LoadFrom method with a Byte[] parameter and Evidence to load a common object file format (COFF) image, only the supplied evidence is used. Evidence of the calling assembly and evidence of the COFF image is ignored.

Example

The following example loads an assembly given its file name or path.

[Visual Basic] 
Dim SampleAssembly As [Assembly]
SampleAssembly = [Assembly].LoadFrom("c:\Sample.Assembly.dll")
' Obtain a reference to a method known to exist in assembly.
Dim Method As MethodInfo = SampleAssembly.GetTypes()(0).GetMethod("Method1")
' Obtain a reference to the parameters collection of the MethodInfo instance.
Dim Params As ParameterInfo() = Method.GetParameters()
' Display information about method parameters.
' Param = sParam1
'   Type = System.String
'   Position = 0
'   Optional=False
Dim Param As ParameterInfo
For Each Param In Params
    Console.WriteLine(("Param=" + Param.Name.ToString()))
    Console.WriteLine(("  Type=" + Param.ParameterType.ToString()))
    Console.WriteLine(("  Position=" + Param.Position.ToString()))
    Console.WriteLine(("  Optional=" + Param.IsOptional.ToString()))
Next Param

[C#] 
Assembly SampleAssembly;
SampleAssembly = Assembly.LoadFrom("c:\\Sample.Assembly.dll");
// Obtain a reference to a method known to exist in assembly.
MethodInfo Method = SampleAssembly.GetTypes()[0].GetMethod("Method1");
// Obtain a reference to the parameters collection of the MethodInfo instance.
ParameterInfo[] Params = Method.GetParameters();
// Display information about method parameters.
// Param = sParam1
//   Type = System.String
//   Position = 0
//   Optional=False
foreach (ParameterInfo Param in Params)
{
    Console.WriteLine("Param=" + Param.Name.ToString());
    Console.WriteLine("  Type=" + Param.ParameterType.ToString());
    Console.WriteLine("  Position=" + Param.Position.ToString());
    Console.WriteLine("  Optional=" + Param.IsOptional.ToString());
}

[C++] 
Assembly* SampleAssembly;
SampleAssembly = Assembly::LoadFrom(S"c:\\Sample.Assembly.dll");
// Obtain a reference to a method known to exist in assembly.
MethodInfo* Method = SampleAssembly->GetTypes()[0]->GetMethod(S"Method1");
// Obtain a reference to the parameters collection of the MethodInfo instance.
ParameterInfo* Params[] = Method->GetParameters();
// Display information about method parameters.
// Param = sParam1
//   Type = System::String
//   Position = 0
//   Optional=False
IEnumerator* myEnum = Params->GetEnumerator();
while (myEnum->MoveNext()) {
   ParameterInfo* Param = __try_cast<ParameterInfo*>(myEnum->Current);

   Console::WriteLine(S"Param= {0}", Param->Name);
   Console::WriteLine(S"  Type= {0}", Param->ParameterType);
   Console::WriteLine(S"  Position= {0}", __box(Param->Position));
   Console::WriteLine(S"  Optional= {0}", __box(Param->IsOptional));
}

[JScript] 
var SampleAssembly : Assembly;
SampleAssembly = Assembly.LoadFrom("c:\\Sample.Assembly.dll");
// Obtain a reference to a method known to exist in assembly.
var Method : MethodInfo = SampleAssembly.GetTypes()[0].GetMethod("Method1");
// Obtain a reference to the parameters collection of the MethodInfo instance.
var Params : ParameterInfo[] = Method.GetParameters();
// Display information about method parameters.
// Param = sParam1
//   Type = System.String
//   Position = 0
//   Optional=False
for (var i : int in Params){
    var Param : ParameterInfo = Params[i];
    Console.WriteLine("Param=" + Param.Name.ToString());
    Console.WriteLine("  Type=" + Param.ParameterType.ToString());
    Console.WriteLine("  Position=" + Param.Position.ToString());
    Console.WriteLine("  Optional=" + Param.IsOptional.ToString());
}

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

.NET Framework Security: 

See Also

Assembly Class | Assembly Members | System.Reflection Namespace | Assembly.LoadFrom Overload List

Show:
© 2015 Microsoft