-2

I have a method similar to this:

public void DoSomething<T>() where T : BaseClass

Which I use as:

DoSomething<MyClass>();

My problem is: I have the already instantiated class stored in a variable, and I would like to use it to call this method.

Exemple: I have a variable to store the class;

BaseClass instantiated;

At some point, I instantiate

instantiated = new MyClass(); //Inherits from BaseClass

At another time, I need to call the method, but all i have is the "instantiated" variable. What do I enter in the method's <>?

DoSomething<?>();
  • 4
    Change your method signature to `public void DoSomething(T obj) where T : BaseClass` and then call it like `DoSomething(instantiated);`. Now inside `DoSomething()` you can work with `obj` however you need. – itsme86 Dec 12 '16 at 18:49
  • What is the T type used for? Can you provide an implementation of the method? – Robba Dec 12 '16 at 18:50
  • Do you know T at compile time? Or only at runtime? – Matt Burland Dec 12 '16 at 18:51
  • 3
    I'm super curious what you are doing inside `DoSomething` that it can effectively do whatever it does given either just a type (that can't be `new()`d up) or an instance. – Jonesopolis Dec 12 '16 at 18:54
  • Are you sure you don't just need an interface and have "DoSomething" depend on the interface instead of it being generic? – gmn Dec 12 '16 at 18:58
  • 1
    Generic methods are for return types. I.e. `public void DoSomething(BaseClass obj)` [achieves the same thing](https://dotnetfiddle.net/bxy77n) as @itsme86. As your return type is `void` then I would assume that this is actually a badly worded [duplicate of this](http://stackoverflow.com/questions/2078914/creating-a-generict-type-instance-with-a-variable-containing-the-type) – Luke Briggs Dec 12 '16 at 19:22
  • Inside I instantiate the class and do what i need. Exemple: var obj = (T)Activator.CreateInstance(typeof(T), param1, param2); – Leonardo Lima Almeida Dec 12 '16 at 19:24
  • 1
    @LeonardoLimaAlmeida That doesn't require a generic method; you could use `public void DoSomething(Type myType)` and then `Activator.CreateInstance(myType,param1,param2) as BaseClass;` instead – Luke Briggs Dec 12 '16 at 19:26
  • I need to instantiate the class inside the method, not out of it. – Leonardo Lima Almeida Dec 12 '16 at 19:27
  • @LukeBriggs, if i use like this, i can't restrict for classes that inherits from BaseClass – Leonardo Lima Almeida Dec 12 '16 at 19:31
  • @LeonardoLimaAlmeida any class that inherited from `BaseClass` could be used in his method example. That's polymorphism. – Jonesopolis Dec 12 '16 at 19:32

1 Answers1

2

Generic methods let us have a custom return type. As you're not using that, i.e. your return type is void, then it doesn't actually need to be a generic method at all:

public void DoSomething(BaseClass myInstance){

    // Instance it and cast to a BaseClass:
    BaseClass obj=(BaseClass)Activator.CreateInstance(myInstance.GetType(),param1,param2);

    // Do anything else with obj.

}

As you say you've already got an existing object, then that is called like so:

DoSomething(thatObject);

Alternatively - this is the more common form - define a virtual Clone() method:

public class BaseClass{

 public virtual BaseClass Clone(){

   return new BaseClass();

 }

}

public class MyClass : BaseClass{

 public override BaseClass Clone(){

   return new MyClass();

 }

}

...

BaseClass myClone=thatObject.Clone();

If we were going to return the newly created object, then that's something a little different. It can make good use of generics:

public T DoSomething<T>() where T:BaseClass{

    // Instance it and cast to T:
    T obj=(T)Activator.CreateInstance(typeof(T),param1,param2);

    // Do anything else with obj.

    return obj;
}

With our call like this:

MyClass myClass=DoSomething<MyClass>();

This can't be done with a runtime known type because then the return type is unknown too.

Here's an example of that last one in action.

Luke Briggs
  • 3,745
  • 1
  • 14
  • 26
  • i agree! And i can call like DoSomething(typeof(MainScreen)), right? This way, i can pass a type that not inherits from BaseClass, which will generate an error. But apparently it is the best way out =/ – Leonardo Lima Almeida Dec 12 '16 at 19:51
  • 1
    @LeonardoLimaAlmeida I've made a few edits which should help some more :) – Luke Briggs Dec 12 '16 at 20:01