-1

I have generic class Name car, Car accepted datatype "Class Type". I get from a database the name of class as string, so I convert string to class type than I pass that class to generic type class, but I always I get error " Error "'o' is a variable but is used like a type". How I can pass Door class to Car generic class.

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string nameOfClass = "ConsoleApplication2.Door";

            Door o = (Door)System.Activator.CreateInstance(Type.GetType(nameOfClass));

            Console.WriteLine("Type:     {0}\nValue:    {1}\nHashCode: {2}\n",
                o.GetType().FullName, o.ToString(), o.GetHashCode());
            Console.ReadLine();
            //error in O 
            Car<o> l = new Car<o>();
        }
    }
    class Car<T> where T : class
    {
        public void carName(T name)
        {}
    }
    class Door
    {}
} 
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
mohammad tofi
  • 207
  • 1
  • 3
  • 14
  • 2
    Why is Car generic? Why are you using Activator to create instances? You've got something wrong with your design if you need to do that. – mason Dec 14 '17 at 19:43
  • You'd have to do `Car` or if you don't know the type before runtime you'd have to create the `Car` class with reflection. I'd say you should rethink your design to avoid reflection if possible. – juharr Dec 14 '17 at 19:45
  • I think you misunderstand what generics are good for. Do some cars have doors and others don't? – Code-Apprentice Dec 14 '17 at 19:47
  • 1
    If you really do have a good case for creating a Car where XXX is only known at runtime it can definitely be done (using `Type.MakeGenericType()`), but as others have suggested, it does tend to suggest you're abusing what Generic types are intended for. – Dylan Nicholson Dec 14 '17 at 19:49
  • Question as asked is duplicate of one of many "create generic from string" (https://www.bing.com/search?q=c%23+create+generic+by+string). Note: as pointed out in comments it is very likely you should be looking for something different (unless `Car` is completely unrelated sample to just have [MCVE]). – Alexei Levenkov Dec 14 '17 at 19:55
  • In my situation, I have library Like Car Class, and I should get the name of class that only I know at runtime than pass this string to a library. so when I pass "o" variable give me the error. – mohammad tofi Dec 14 '17 at 20:00
  • Because `o` isn't a type. It's the name of an instance of a Door. – mason Dec 14 '17 at 20:08

1 Answers1

0

Generics are for when you have an class that can use another object of any type. For example, a List can contain any type of object, whether it's a Car or something else. On the other hand, a car will always have more than one door. This suggests that a Car class should have more a Door member field, or maybe a List<Door> so that it can have an indeterminate number of doors.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Thanks for reply but I give the car as an example and my question about the error that I get when I pass Door to Car class. – mohammad tofi Dec 14 '17 at 20:04
  • @mohammadtofi If you want to pass the **door** object to the `Car` constructor, then you need to do `Car c = new Car(o);`. Note that there are no angle brackets `<>` in this code. – Code-Apprentice Dec 14 '17 at 20:08
  • when I try it, it gives me the error : "Using the generic type 'Car' requires 1 type arguments" – mohammad tofi Dec 14 '17 at 20:12
  • 1
    Which brings us back to my original comment: you are using generics incorrectly. Car should not need a generic type at all. Change `class Car where T : class` to `class Car`. – Code-Apprentice Dec 14 '17 at 20:14