C# Reflection: How to Get Class Reference from String

C# Reflection: How to get class reference from string?

You will want to use the Type.GetType method.

Here is a very simple example:

using System;
using System.Reflection;

class Program
{
static void Main()
{
Type t = Type.GetType("Foo");
MethodInfo method
= t.GetMethod("Bar", BindingFlags.Static | BindingFlags.Public);

method.Invoke(null, null);
}
}

class Foo
{
public static void Bar()
{
Console.WriteLine("Bar");
}
}

I say simple because it is very easy to find a type this way that is internal to the same assembly. Please see Jon's answer for a more thorough explanation as to what you will need to know about that. Once you have retrieved the type my example shows you how to invoke the method.

Get class by string value

Yes, use Reflection.

You can use Type.GetType to get an instance of the Type for the class by string, then instantiate it using Activator.CreateInstance, something like this:

public MyObject Create(string pattern)
{
Type t = Type.GetType(pattern);
if (t == null) {
throw new Exception("Type " + pattern + " not found.");
}
return Activator.CreateInstance(t);
}

You could use Activator.CreateInstance(string, string) overload also, but this would not directly return a new instance of the Type required.

Getting Class object from its string name in C#

Try using the Assembly Qualified Name instead. If it's running Xamarin, it might need to use the full qualified name. I've had the same limitations with Silverlight.

To quickly get the full name, just reference the type and output it:

Console.WriteLine(typeof(TempIOAddon).AssemblyQualifiedName);

EDIT: Here's the MSDN doc for Type.GetType(string) for Silverlight. Since Xamarin's mobile platform is more or less based off it, generally the docs can apply: http://msdn.microsoft.com/en-us/library/w3f99sx1%28v=vs.95%29.aspx

Note that likely if the type is in the currently executing assembly, you'll probably only need the full namespace.name

Get class properties by class name as string in C#

I suspect you're looking for:

Type type = Type.GetType("User");
Object user = Activator.CreateInstance(type);

Note:

  • This will only look in mscorlib and the currently executing assembly unless you also specify the assembly in the name
  • It needs to be a namespace-qualified name, e.g. MyProject.User

EDIT: To access a type in a different assembly, you can either use an assembly-qualified type name, or just use Assembly.GetType, e.g.

Assembly libraryAssembly = typeof(SomeKnownTypeInLibrary).Assembly;
Type type = libraryAssembly.GetType("LibraryNamespace.User");
Object user = Activator.CreateInstance(type);

(Note that I haven't addressed getting properties as nothing else in your question talked about that. But Type.GetProperties should work fine.)

Having a string representation of a class name how do I pass it to a generic

See Andrew & Jon's answers on this question.

C# Reflection: How to get class reference from string

Someone should probably mark this as duplicate.

I have a C# class which i have to get the type of... but i only have the name(single name) as a string

Sadly Type.GetType(string) is quite finicky about the details it requires before it will give you the result. If the type you're looking for is in current executing assembly (or mscorlib/System.Private.CoreLib) then a fully specified name (including namespace) will get the result. If it's in a different assembly then you need to specify the assembly name.

Type type = null;

// Fails
type = Type.GetType("String");

// Succeeds from core library
type = Type.GetType("System.String");

// Fails (loading from a different assembly)
type = Type.GetType("System.Text.Json.JsonSerializer");

// Succeeds (.NET 6.0)
type = Type.GetType("System.Text.Json.JsonSerializer, System.Text.Json");

Which is all well and good until you're trying to find a type that you have exactly one piece of information about: the class name. Without namespace and assembly name you're stuck with enumerating every type until you find what you're looking for...

static Type? TypeFromName(string name) =>
// All loaded assemblies
AppDomain.CurrentDomain.GetAssemblies()
// All types in loaded assemblies
.SelectMany(asm => asm.GetTypes())
// Return first match
.FirstOrDefault(type => type.Name == name);

Simple, yes. Fast? Not even remotely. But you only need to do the search once and cache the results for later use.



Related Topics



Leave a reply



Submit