Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why I need to use Type.GetType() when I make new instance of Type ? Instead of this:
C#
Type T = Type.GetType("CSharp_Practicing.Customer");

Why I can't just do this ?
C#
Type T = Customer;


What I have tried:

Asking question here in CodeProject.com
Posted
Updated 21-Jul-17 11:28am
Comments
Ralf Meier 21-Jul-17 3:36am    
You can't use that assignment because Customer is your class (Object) - not the type of your class.
If you want to get the Type you should ask for it - that is the difference ...
BillWoodruff 21-Jul-17 17:52pm    
Direct use of 'GetType() and 'typeof() usually occur when dealing with generic classes and methods. See my answer here for examples.

Because Customer does not derive from Type - and you can't create a new instance of a class just by using the class name.
You can do this:
C#
Type T = typeof(Customer);
And that will do the same as using GetType.
GetType is normally used for getting a Type based on a string based name at runtime, rather than a fixed type at compile time.
 
Share this answer
 
Because they are different beasts. Acccording to the documentation[^]:
Quote:
Type is the root of the System.Reflection functionality and is the primary way to access metadata. Use the members of Type to get information about a type declaration, about the members of a type (such as the constructors, methods, fields, properties, and events of a class), as well as the module and the assembly in which the class is deployed.


while Customer is just a type declaration.
 
Share this answer
 
If you have an instance of a Class, you can call instance .GetType() on it. If you simply have a reference to a valid Type identifier ... it's name ... you can call 'typeof(name).
using System;

namespace Whatever
{
    public class TypeFun1
    {
        private string Name;

        public TypeFun1(string name)
        {
            Name = name;
        }
    }

    public class TypeFun2
    {
        private string Name;

        public TypeFun2(string name)
        {
            Name = name;
        }
    }

    public static class FunFactory<T>
    {
        public static T MakeNewT()
        {
            switch (typeof(T).Name)
            {
                case "TypeFun1":
                    return (T) Activator.CreateInstance(typeof(T), "me one");
                case "TypeFun2":
                    return (T) Activator.CreateInstance(typeof(T), "me two");
                default:
                    return default(T);
            }
        }
    }
}
Test:
public void testClassCreationMethods()
{
    var tclass1 = new TypeFun1("me one");

    var tclass2 = new TypeFun2("me two");

    var tfromfactory1 = FunFactory<TypeFun1>.MakeNewT();

    var tfromfactory2 = FunFactory<TypeFun2>.MakeNewT();

    bool isTypeFun1 = tclass1.GetType() == typeof(TypeFun1); // true

    bool isSameTypes = tfromfactory1.GetType() == typeof(TypeFun1); // true

    bool sametypr = tclass1.GetType() == tfromfactory1.GetType(); // true
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900