只有通用参数时调用静态成员 [英] Calling static member when you have only generic parameter

查看:49
本文介绍了只有通用参数时调用静态成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我只有一个通用参数时,可以用任何方法在类型上调用静态成员.例如,如果我有这样的东西

 公共Get< T>(int id){//我想这样做字符串s = T.SomeMethodName();} 

我可以这样做,但是有点令人讨厌",然后不管它是否是静态的都没有关系.或者我可以按照尤里(Yuriy)的建议使用反射.

  ISomeKnownInterface i =(ISomeKnownInterface)new T();字符串s = i.SomeMethodName(); 

现在的问题是哪种方法更好,使用反射创建新的实例

  public TFormDto Get< TFormDto>(intentityId)其中TFormDto:AbstractEntityFormDto,new(){//创建新实例AbstractEntityFormDto a =(AbstractEntityFormDto)new TFormDto();字符串entityName = a.GetEntityFullTypeName();//使用反射类型type = typeof(TFormDto);字符串entityName = type.GetMethods(BindingFlags.Public | BindingFlags.Static).Single(m => m.Name =="GetEntityFullTypeName").invoke(null,null); 

解决方案

不是始终都是AbstractBaseClass.GetFullName().否则,您必须在T上使用反射以获取另一个类的静态方法.可能会有所帮助./p>

以下是一个简单的示例:

  class TestClass{公共静态无效Hello(){Console.WriteLine("World !!!");}}公共静态无效性Test< T>(),其中T:class{类型type = typeof(T);type.GetMethods(BindingFlags.Public | BindingFlags.Static).Single(m => m.Name =="Hello").invoke(null,null);} 

在您的示例中,我假设您知道接口没有静态方法.我以为您的意思是您有一个为静态方法签名的接口,而实现该接口的类只会从实现中调用静态方法.那样也可以,但是您不能保证将调用正确的static并确保T在该接口上有约束,而不仅仅是new().

Ts there any way to call a static member on 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 is kinda "yucky", and then it does not matter if it is static or not. Or I could use reflection as suggested by Yuriy.

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

So question is now which is better approach, creating new instance of 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);

解决方案

Wouldn't that just always be AbstractBaseClass.GetFullName(). Otherwise you have to use reflection on T to get the static method of another class. This might be helpful.

The following 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 assume you know interfaces don't have static methods. I assumed you meant you had an interface which has a signature for the static method and a class implementing said interface would just call the static method from the implementation. That would work as well, but you're not guaranteed the correct static will be called and make sure T has a constraint on that interface, not just new().

这篇关于只有通用参数时调用静态成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆