Type.Gettype("Namespace.A.B.Classname") Returns Null

Type.GetType(namespace.a.b.ClassName) returns null

Type.GetType("namespace.qualified.TypeName") only works when the type is found in either mscorlib.dll or the currently executing assembly.

If neither of those things are true, you'll need an assembly-qualified name:

Type.GetType("namespace.qualified.TypeName, Assembly.Name")

Type.GetType(string typeName) returns null

If you know that whatever type it is will be within DataAccessLayer, then I'd get an Assembly reference as simply as possible, e.g.

 Assembly assembly = typeof(AnyPublicTypeWithinTargetAssembly).Assembly;
Type type = assembly.GetType(namespaceQualifiedTypeName);

An alternative is to use Type.GetType with an assembly-qualified name, but that's more long-winded in terms of specifying the type name.

C# : Type.GetType is null on classes of dynamically loaded assemblies

I figured it out. I cannot believe how simple the solution was. After some probing in the AppDomain.CurrentDomain and some MSDN documentation I just needed to implement and add a custom assembly resolve event handler to the current domain.

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(currentDomain_AssemblyResolve);


static Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
--args.Name is the assembly name passed to the GetType("FullClassName,AssemblyName");
--Search in LoadedAssemblies by name and return it.
}

Type.GetType(System.Net.WebException) returns null

According to the docs of Type.GetType, you can only pass a namespace-qualified type name if the type is in mscorlib.dll, otherwise you have to use the assembly-qualified name. WebException is not in mscorlib.dll, so you at least need:

Type.GetType("System.Net.WebException, System, Version=4.0.0.0")

By inspecting typeof(WebException).AssemblyQualifiedName, you can see that is something like:

System.Net.WebException, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

Type.GetType not working

The reason that Type.GetType("System.Uri") returns null is that the type is located in system.dll instead of mscorlib.dll. You must use the assembly-qualified name as noted above.

From MSDN:

typeName
Type: System.String

The assembly-qualified name of the type to get. See AssemblyQualifiedName.
If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.

Type.GetType() returning null

Type.GetType(string) only looks in the currently executing assembly and mscorlib when you don't specify the assembly name within the string.

Options:

  • Use the assembly-qualified name instead
  • Call Assembly.GetType(name) on the appropriate assembly instead

If you have an easy way of getting hold of the relevant assembly (e.g. via typeof(SomeKnownType).Assembly) then the second option is probably simpler.

Loading C# DLL with powershell, [System.Type]::GetType returns null

  • Use Add-Type -Path to load your assembly.

  • After loading, to get a reference to that assembly's [ClassLibrary1.Class1] type as a [Type] instance (to use for reflection) via a string variable, simply cast to [Type].

The following corrected and annotated version of your code demonstrates the approach:

# Compile the C# source code to assembly .\ClassLibrary1.dll
Add-Type -TypeDefinition @'
namespace ClassLibrary1
{
public class Class1
{
public static void Test()
{
System.Diagnostics.Process.Start("CMD.exe", "/C calc");
}
}
}
'@ -OutputAssembly .\ClassLibrary1.dll


# Define the path to the assembly. Do NOT use "\\" as the path separator.
# PowerShell doesn't use "\" as the escape character.
$Path = ".\ClassLibrary1.dll"
$Namespace = "ClassLibrary1"
$ClassName = "Class1"
$Method = "Test"
$Arguments = $null

# Load the assembly by its filesystem path, using the Add-Type cmdlet.
# Use of relative paths works.
Add-Type -Path $Path

$Full_Class_Name = "$Namespace.$ClassName"

# To get type [ClassLibrary1.Class1] by its full name as a string,
# simply cast to [type]
$Type = [type] $Full_Class_Name

$MethodInfo = $Type.GetMethod($Method)
$MethodInfo.Invoke($null, $Arguments)

As for what you tried:

As PetSerAl points out, [System.Type]::GetType() can only find a given type if:

  • its assembly is already loaded
  • its assembly can be located via standard assembly resolution, as detailed here, which involves many complicated rules, the only two of which likely apply to your scenario is if you're trying to reference a built-in type from the mscorlib assembly, or an assembly located in the GAC (Global Assembly Cache).

    • By definition, only strongly-named assemblies - those signed with a private key matching the public key mentioned in the assembly's full name - can be placed in the GAC.

While you could have called [System.Reflection.Assembly]::LoadFile($Full_Path) first in order to load the assembly via its filesystem path, after which [System.Type]::GetType($Type_Name) would have succeeded, it is ultimately simpler - and more PowerShell-idiomatic - to use Add-Type -Path to load the assembly (which has the added advantage of not requiring a full (absolute) file path), and, once loaded, to use [Type] with just the type's full name (no reference to the assembly needed anymore).

Type.GetType return null

You need to add the assembly name as well, since your type isn't in the executing assembly (nor mscorlib.) So the call should be:

var myType = Type.GetType("caLibClient.entity.Web2ImageEntity, FullAssemblyName");

From the Type.GetType() docs:

typeName

Type: System.String

The assembly-qualified name of the type to get. See
AssemblyQualifiedName. If the type is in the currently executing
assembly or in Mscorlib.dll, it is sufficient to supply the type name
qualified by its namespace.

From the docs for AssemblyQualifiedName, this is a sample name:

TopNamespace.SubNameSpace.ContainingClass+NestedClass, MyAssembly, Version=1.3.0.0, Culture=neutral, PublicKeyToken=b17a5c561934e089

Update: If you're already referencing the assembly in your project, and know at compile-time what the type-name is, you're better off saying

Type myType = typeof(caLibClient.entity.Web2ImageEntity);

since now you don't need to search for the type at run-time; the compiler will do everything for you.

Invoking GetMethod() on a System.__ComObject always returns null

This cannot work, COM does not support Reflection. So you cannot expect Type.GetMethod() to return anything. Late binding in COM is one-way, you can ask the server to execute a method by name but it won't tell you what methods it supports and what arguments they take. You are supposed to know this from the documentation.

You actually have a type library so technically you can inspect it at runtime to see what it is inside. This is far beyond painful and utterly impractical. Far simpler is to just add a reference to the type library. And you'll get auto-generated .NET wrapper types that you can use directly in your code. Check the MSDN article about Tlbimp.exe for details. This turns on lots of goodies, like IntelliSense and compile-time error checking. The code runs a lot faster as well. No need for dynamic anymore. The only disadvantage is that you'll bind your code to a specific version of the COM server, the one you have the type library for. But you'll of course still crash when your late-bound call is not compatible with an update, there is no magic cure for that.



Related Topics



Leave a reply



Submit