Calling a static member when you only have a shared parameter

Ts is there any way to call a static member by type when I only have a generic parameter. For example, if I have something like this

public Get<T>(int id)
{
   // I would like to do this
   string s = T.SomeMethodName();
}

      

I could do it like this, but it's "yucky" and then it doesn't matter if it's static or not. Or I could use the reflection suggested by Yuri.

ISomeKnownInterface i = (ISomeKnownInterface ) new T(); 
string s = i.SomeMethodName();

      

So now the question is the best approach creating a new instance using reflection

public TFormDto Get<TFormDto>(int entityId) where TFormDto : AbstractEntityFormDto, new()
        {
// create new instance
            AbstractEntityFormDto a = (AbstractEntityFormDto) new TFormDto();
            string entityName = a.GetEntityFullTypeName();

// use reflection 

            Type type = typeof(TFormDto);
            string entityName = type.GetMethods(BindingFlags.Public | BindingFlags.Static)
                .Single(m => m.Name == "GetEntityFullTypeName")
                .Invoke(null, null);

      

+2


source to share


3 answers


Wouldn't it always be AbstractBaseClass.GetFullName (). Otherwise, you must use reflection on T to get a static method of another class. This might be helpful.

Below is a simple example:



class TestClass
{
    public static void Hello()
    {

        Console.WriteLine("World!!!");
    }
}

public static void Test<T>() where T : class
{
    Type type = typeof(T);
    type.GetMethods(BindingFlags.Public | BindingFlags.Static)
        .Single(m => m.Name == "Hello")
        .Invoke(null, null);

}     

      

With your sample, I will assume that you know that interfaces do not have static methods. I assumed you meant that you have an interface that has a signature for a static method, and a class that implements said interface will just call the static method from the implementation. This will work too, but you are not guaranteed to call the correct static code and make sure T has a constraint on that interface, not just new ().

+1


source


The problem is not that it T

has a static member method named SomeMethodName

. If you want to AbstractBaseClass.SomeMethodName

make a call , do so. The rationale for why this is not possible is extending the methods of the static interface methods .



+1


source


There is no way to do this without using reflection. To call a static method, you always need to specify the actual type.

If you want to improve the compile-time safety of your code, you will try to avoid reflection. Make the method non-static and place it in an interface. This will most likely (I don't know your problem) will be the cleanest solution.

I've made quite a few non-static members to take advantage of interfaces and polymorphism. Sometimes I implement something similar to the singleton pattern (like a static property to get an instance), or just call the default constructor to get an instance and pass it as an argument.

interface ISomeInterface
{
  string SomeMethodName();
}

class SomeImplementation : ISomeInterface
{
  public static Instance { get { return new SomeImplementation(); } }

  public string SomeMethodName()
  {
    // implementation of SomeMethodName
  }
}

public Get(ISomeInterface i)
{
   string s = i.SomeMethodName();
}

public Example()
{
  Get(SomeImplementation.Instance);
}

      


You can also create a new instance in a generic method, you can also specify that you need a default constructor:

public Get<T>() where T : ISomeInterface, new()
{
   T instance = new T();
   string s = instance.SomeMethodName();
}

public Example()
{
  Get<SomeImplementation>();
}

      

0


source







All Articles