0

I am beginner of C#, and have problem that keep bothers me.

I will very appreciated if someone makes a solution.

I have a code something like :

public class SomeClass 
{ 
  public SomeClass()
  {
    GenericMethod<Object1>(out result);
    ....
  }
  public void GenericMethod<T>(out Result result) where T : Parent
  {
     T t = new T();
     //do something need to be done all parent's child object ( Object1, Object2..)
     ...somthing same things...
     //and do specific things to be done relatively
     Handle(t, result);
  }
  // Object1 is child of Parent
  private void Handle(Object1 arg1, out Result result)
  {
    //do something need to be done for Object1;
  }
  // Object1 is child of Parent
  private void Handle(Object2 arg1, out Result result)
  {
    //do something need to be done for Object2;
  }
  ....
}

as you will discover, what I want to do is simple.

make specified value with Type T in GenericMethod, and invoke the specified Handle Method. It will works in C++, but C# keep telling me like

"cannot convert 'T' expression to type 'Object1'"

How can I solve this problem ? Thanx in advance.

nolleh
  • 107
  • 2
  • 10
  • 1
    When your method is not intrinsically generic, Why do you need a generic method? *Generic means whatever the type of the object may be, I'm going to do the same thing*. In your case it is not. – Sriram Sakthivel Aug 26 '14 at 09:29
  • 1
    What would you want to happen if someone called `GenericMethod` or some other type that wasn't handled? – Jon Skeet Aug 26 '14 at 09:29
  • That's just the normal behavior as your T object can be any type with a default constructor. – timothy.B Aug 26 '14 at 09:33
  • @SriramSakthivel actually my generic is not for all type, but in type which derived from Parent, do same thing, and have a slightly do different things. that was What I wanted. Um.. – nolleh Aug 26 '14 at 10:09
  • @Jon Skeet may be my example is not enough to explain my situation. sorry, I've modified. – nolleh Aug 26 '14 at 10:09
  • @timothy.B um may be dburners explanation explain your words more explanationally. thanx. – nolleh Aug 26 '14 at 10:10
  • @nolleh Yeah, sorry I didn't have the C++ background needed to understand that generic method are not build at runtime. My bad. – timothy.B Aug 26 '14 at 10:14

1 Answers1

1

Thats because C++ generic methods and types are constructed at compile type. So when you call GenericMethod the compiler will build that code for you, and will know whitch Handle method to call.

The C++ code will look like this after the compiler builds the method for you:

public class SomeClass 
{ 
  public SomeClass()
  {
    GenericMethod<Object1>(out result);
    ....
  }
  public void GenericMethod<Object1>(out Result result)
  {
     Object1 t = new Object1();
     Handle(t, result);
  }
  private void Handle(Object1 arg1, out Result result)
  {
    //do something need to be done for Object1;
  }
  private void Handle(Object2 arg1, out Result result)
  {
    //do something need to be done for Object2;
  }
  ....
}

As you can see it is now very clear what Handle method the compiler will use.

C# generics are compiled at runtime. The compiler sees a call to a Handle method with an unkown type, so the compiler complains when you want to call a method with an unkonwn type at compile time.

You can actualy use the Handle method in C# with a generic if T is the type from the parameter like so:

public void GenericMethod<T>(out Result result) where T : new(), BaseClass
{
     T t = new T();
     Handle(t, result);
}
private void Handle(BaseClass arg1, out Result result)
{
    //do something need to be done for BaseClass;
}

For more information about C# and C++ generics you can take a look here

dburner
  • 1,007
  • 7
  • 22
  • thanx. If C# Generic is generated in runtime, may be I have no choice but do need to be done to BaseClass is invoked in specified Handle class.. It will be private void Handle(Object1 arg1, out Result result) { doSameThingForBaseClass(); } private doSameThingForBaseClass(){ } – nolleh Aug 26 '14 at 10:20