Chandrahas Balleda
Chandrahas Balleda

Reputation: 2822

How does Type T = Type.GetType("SomeClass"); give me an object?

When I am going through reflection I came across this:

Type T = Type.GetType("Somenamespace.someclass");

Is 'T' an object here? If so, it lacks the complete definition of object as:

Type T = new Type();

Then how can T be an object? And how can we assign the value of Type.GetType("Somenamespace.someclass"); into T?

Upvotes: 2

Views: 1576

Answers (3)

nozzleman
nozzleman

Reputation: 9649

T is an object of Type. It is not an instance of someclass, if that was what you wanted to know.

The Type-class is used to describe class-declarations and some more.

To address your comment:

Type T = new Type();

can only be called by other Constructors of Type as well as classes that inherit from Type, as the parameterless Type-Constructor is declared protected, and it would not make sense to call it in normal code (what would it describe?). A typical call woud be sth. like that:

var obj = new someclass();
var type = obj.GetType();

Upvotes: 3

Ali Adlavaran
Ali Adlavaran

Reputation: 3735

Use the Activator.CreateInstance() class. such this:

var someclassType = Type.GetType(“Somenamespace.someclass”);
var someclassInstance = Activator.CreateInstance(someclassType);

And another example :

using System;

class DynamicInstanceList
{
    private static string instanceSpec = "System.EventArgs;System.Random;" +
        "System.Exception;System.Object;System.Version";

    public static void Main()
    {
        string[] instances = instanceSpec.Split(';');
        Array instlist = Array.CreateInstance(typeof(object), instances.Length);
        object item;
        for (int i = 0; i < instances.Length; i++)
        {
            // create the object from the specification string
            Console.WriteLine("Creating instance of: {0}", instances[i]);
            item = Activator.CreateInstance(Type.GetType(instances[i]));
            instlist.SetValue(item, i);
        }
        Console.WriteLine("\nObjects and their default values:\n");
        foreach (object o in instlist)
        {
            Console.WriteLine("Type:     {0}\nValue:    {1}\nHashCode: {2}\n",
                o.GetType().FullName, o.ToString(), o.GetHashCode());
        }
    }
}

// This program will display output similar to the following: 
// 
// Creating instance of: System.EventArgs 
// Creating instance of: System.Random 
// Creating instance of: System.Exception 
// Creating instance of: System.Object 
// Creating instance of: System.Version 
// 
// Objects and their default values: 
// 
// Type:     System.EventArgs 
// Value:    System.EventArgs 
// HashCode: 46104728 
// 
// Type:     System.Random 
// Value:    System.Random 
// HashCode: 12289376 
// 
// Type:     System.Exception 
// Value:    System.Exception: Exception of type 'System.Exception' was thrown. 
// HashCode: 55530882 
// 
// Type:     System.Object 
// Value:    System.Object 
// HashCode: 30015890 
// 
// Type:     System.Version 
// Value:    0.0 
// HashCode: 1048575

Upvotes: 0

jjczopek
jjczopek

Reputation: 3379

Yes, T is an object of class Type, which:

Represents type declarations: class types, interface types, array types, value types, enumeration types, type parameters, generic type definitions, and open or closed constructed generic types. https://msdn.microsoft.com/en-us/library/System.Type%28v=vs.110%29.aspx

Type.GetType("Somenamespace.someclass");

is a static method of Type class, which will return the Type object with properties and methods that are relevant to this particular class. It will not be an instacne of Somenamespace.someclass.

Upvotes: 2

Related Questions