1

Is it possible to create a method which returns a type and use it?

for example, assume I have a Person type and Object parameter. As you probably know, If we want to cast our Object parameter, we can write:

object param;

((Person)param).executePersonMethod();

My question is how can I write a method which return Person type and use it instead the concrete Person cast - I want write something like this:

public Type GetPersonType()
{
//return here type of person
}

and then use

  ((GetPersonType())param).executePersonMethod();

Is it possible? If yes, how?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Ofir
  • 5,049
  • 5
  • 36
  • 61

7 Answers7

4

You can use interface.

((IPerson)param).executePersonMethod();

each type of person must be an IPerson and in IPerson you declare executePersonMethod()

Pascalz
  • 2,348
  • 1
  • 24
  • 25
  • Hi, I know this approach - it not good for my scenario, I need more dynamic solution. I don`t want create seperate Interface for any type – Ofir Sep 19 '13 at 07:38
2

You can also use dynamic for this.

Note that using dynamic will skip any compile time checking whether that method exists and will throw an exception at runtime if it doesn't.

Due to this risk, I would only do this if I have no other choice, but it's good to know that the option exists:

dynamic d = param;
d.executeWhateverMethodHereWithoutCompileTimeChecking();
futankamon
  • 125
  • 3
2

Yes you can. There is a new type called dynamic which will avoid static type check during compilation.

public dynamic GetPersonOrObjectWhichHasExecutePersonMethod()
{
 //return not the type but the object itself
 return new Person(); 
}

public class Person
{
    public void executePersonMethod()
    {
      // do something
    }
}

// this is how you invoke it
public void ExecuteMethod()
{
  dynamic obj = GetPersonOrObjectWhichHasExecutePersonMethod();
  obj.executePersonMethod();
}
Lav G
  • 153
  • 9
1

Maybe you can use something like that:

Convert.ChangeType(param, typeof(Person)); 

It would returns param as a Person.

Ricardo Rodriguez
  • 1,010
  • 11
  • 22
0

Use

TypeOf()

Example :

        if (data.GetType() == typeof(Personne))
            return (Personne)data;
        else
            return new Personne();

After, check your object is not null to know if it's ok

CheapD AKA Ju
  • 713
  • 2
  • 7
  • 21
0

Although you can use Generic as a better option, however it is possible using Type Convertor in combination with reflection:

Type type = GetPersonType();
var converted = Convert.ChangeType(param, type);
converted
     .GetType()
     .GetMethod(/*your desired method name in accordance with appropriate bindings */)
     .Invoke(converted, /* your parameters go here */);
Alireza
  • 10,237
  • 6
  • 43
  • 59
0

You cannot execute methods on a Type, you can only execute methods on an instance of a particular Type. If I am understanding correctly, you should be able to use the answer @LavG gives to:

return not the type but the object itself from the GetPersonType method

Edit: As per your comment:

  • Here are some SO QA which will help you get the Type using fully qualified namespace and other techniques:

How to get the type for a class by sending just the name of the class instead of the class itself as the parameter?

Type.GetType("namespace.a.b.ClassName") returns null

  • Here is how to generate a class from the given Type at run time:

generating a class dynamically from types that are fetched at runtime

Community
  • 1
  • 1
S2S2
  • 8,322
  • 5
  • 37
  • 65