如何拥有没有任何类型限制的通用外参数

发布于 2024-12-20 18:03:16 字数 5213 浏览 2 评论 0 原文

问题是这样的: 我想要一个具有泛型类型的外参数的泛型函数。 将泛型类型限制为 ref-type 当然没有问题。 但我想要一个完全不受限制的泛型类型!没有 new() 或类/结构限制!

public class A
{ }

public class B<T> : A
{
  public T t;
  public B(T i)
  {
    this.t = t;
  }
}

public static class S
{
  public static bool Test<T>(ref A a, out T t)
  {
    C<T> c = a as C<T>;
    if(c != null)
    {
      t = c.t;
      return true;
    }
    else
      return false;
  }
}

class Run
{
  static void Main(string[] args)
  {
    C<SomeType> c = new C<SomeType>(new SomeType(...));

    SomeType thing;
    S.Test<SomeType>(c,thing);
  }
}

上面的代码说明了我想要做什么。我想设置输出参数,但仅限于与所描述的类似的条件下。在 Test(...) 的错误情况下,我对 out t 的值完全不感兴趣。但上面的代码当然不是工作代码。 上述问题是 out 参数必须被初始化。但也许初始化有时很昂贵(取决于 T 的类型),而且我不想初始化一个虚拟类实例只是为了让编译器停止抱怨。所以问题就变成了:如何初始化一个未知类型(并确保如果它是一个类,它被初始化为 null)?

理论上你应该能够写出类似的东西

public static bool Test<T>(ref A a, out T t)
{
  //...
  else
  {
    if(typeof(T).IsValueType)
    {
      t = (T)0; //Can't cast from int to T error (Yeah, this is a little tricky...)
      //OR:
      t = new T(); //No-no, doesn't have the new()-restriction on T (But we know it's a value type... life sucks)
    }
    else
      t = null; //Error: Can't set to null! T could be valueType! (No it CAN'T... again life sucks)
    return false;
  }
}

,但可惜它并不那么简单。第一个问题是当 T 是值类型时,我们应该能够创建它,但编译器不允许我们创建它。第二个问题类似:“它可能是一个值类型!” - 不,我只是确定不是这样。它应该可以工作,但是却没有。很烦人。

好的。所以我们开始发挥创意......毕竟,有一个名为 Object 的好类,它与 C# 的所有事物都有特殊的关系。

public static bool Test<T>(ref A a, out T t)
{
  //...
  else
  {
    if(typeof(T).IsValueType)
    {
      t = (T)(object)0; //Works ONLY if T is valuetype: int, otherwise you get a "must be less than infinity"-error.
    }
    else
    {
      t = (T)(object)null; //Null ref exception in the cast...
    }
    return false;
  }
}

这至少可以编译。但它仍然是垃圾。运行时错误很多。 值类型的问题在于,对象类型会记住它实际上是什么类型,并且当尝试转换为其他类型时......奇怪的事情发生了(无穷大?真的吗??) 好吧,这该死的应该是可行的!所以,让我们更有创意吧!

public static bool Test<T>(ref A a, out T t)
{
  //...
  else
  {
    if(typeof(T).IsValueType)
    {
      //... still rubbish here
    }
    else
    {
      object o = null;
      t = (T)o; //Tricked you stupid compiler!
    }
    return false;
  }
}

这是正确的!它看起来是一个愚蠢的、微不足道的改变......但是这个编译 - 并且对于非值类型,它运行并且给出了我们想要的结果!如果 T 是引用类型,它将被初始化为 null。 仍然是值类型的问题。创造力有些不情愿地把注意力转向了反思。在对反射的东西进行了一些随机挖掘之后,寻找一些值得尝试的东西(不!你无法获得值类型的构造函数,它返回 null),我在 msdn 上偶然发现了一条小注释:

“创建没有实例的值类型的实例 构造函数,使用 CreateInstance 方法。”

输入 CreateInstance() - http://msdn.microsoft.com/en-us/library/0hcyx2kd.aspx

“CreateInstance泛型方法被编译器用来实现 由类型参数指定的类型的实例化。”

现在我们已经取得进展了!当然它确实说

“一般情况下,应用程序中的CreateInstance没有什么用处 代码,因为类型必须在编译时已知。如果类型是 在编译时已知,可以使用正常的实例化语法(新 C# 中的运算符、Visual Basic 中的 New、C++ 中的 gcnew)。”

但是,嘿 - 我们并没有完全做一般的事情,我们处于创造性模式,编译器对我们很脾气。完全有理由尝试一下。AND

public static bool Test<T>(ref A a, out T t)
{
  //...
  else
  {
    if(typeof(T).IsValueType)
    {
      t = Activator.CreateInstance<T>(); //Probably not your everyday code...
    }
    else
    {
      object o = null;
      t = (T)o;
    }
    return false;
  }
}

BAM就是这样!效果非常好! 在VS2010SP1和MonoDevelop(使用Unity3.4)中测试和运行的代码

下面是一些使用System

namespace GenericUnrestrictedOutParam
{
    class Program
    {
        class TestClass
        {
            public int i;
        }

        struct TestStruct
        {
            int i;
            TestClass thing;
        };

        public static void NullWorkaround<T>(out T anything)
        {
            if (typeof(T).IsValueType)
            {
                anything = Activator.CreateInstance<T>();
            }
            else
            {
                object o = null;
                anything = (T)o;
            }
        }

        static void Main(string[] args)
        {
            int i;
            float f;
            string s;
            TestStruct ts;
            TestClass c;

            NullWorkaround<int>(out i);
            NullWorkaround<float>(out f);
            NullWorkaround<string>(out s);
            NullWorkaround<TestStruct>(out ts);
            NullWorkaround<TestClass>(out c);
        } //Breakpoint here for value-checking
    }
}

;还有光荣的“输出”(来自 locals-panel @breakpoint):

        args    {string[0]} string[]
        i   0   int
        f   0.0 float
        s   null    string
-       ts  {GenericUnrestrictedOutParam.Program.TestStruct}    GenericUnrestrictedOutParam.Program.TestStruct
          i 0   int
          thing null    GenericUnrestrictedOutParam.Program.TestClass
        c   null    GenericUnrestrictedOutParam.Program.TestClass

即使是带有值和类类型的结构也得到了精美的处理:值类型为 0,类实例为 null。 任务完成!

The problem is this:
I want a generic function that has an out-parameter of the generic type.
Restrict the generic type to ref-type and there is no problem ofcourse.
But I wanted a totally unrestricted generic type! No new() or class/struct-restrictions!

public class A
{ }

public class B<T> : A
{
  public T t;
  public B(T i)
  {
    this.t = t;
  }
}

public static class S
{
  public static bool Test<T>(ref A a, out T t)
  {
    C<T> c = a as C<T>;
    if(c != null)
    {
      t = c.t;
      return true;
    }
    else
      return false;
  }
}

class Run
{
  static void Main(string[] args)
  {
    C<SomeType> c = new C<SomeType>(new SomeType(...));

    SomeType thing;
    S.Test<SomeType>(c,thing);
  }
}

The above code illustrates what I want to do. I want to set the out parameter, but only under a similar condition as the one depicted. In the false-case of Test(...) I'm totally uninterested in the value of out t. But the above is ofcourse not working code.
The problem above is that an out parameter must be initialized. But maybe initialization is sometimes expensive (depends on type of T) and I don't want to initialize a dummy class instance just to make the compiler stop complaining. So the question then becomes: How do you initialize an unknown type (and making sure it is initialized to null if it's a class)??

Well in theory you should be able to write something like

public static bool Test<T>(ref A a, out T t)
{
  //...
  else
  {
    if(typeof(T).IsValueType)
    {
      t = (T)0; //Can't cast from int to T error (Yeah, this is a little tricky...)
      //OR:
      t = new T(); //No-no, doesn't have the new()-restriction on T (But we know it's a value type... life sucks)
    }
    else
      t = null; //Error: Can't set to null! T could be valueType! (No it CAN'T... again life sucks)
    return false;
  }
}

But alas it's not so simple. The first problem is when T is value-type, we should be able to create it but compiler wont let us. The second problem is similar: "It could be a value-type!" - no, I just made sure it wasn't. It should work but it doesn't. Very annoying.

Ok. So we start to go creative... after all, there is this nice class called Object, and it has a special relation to all things C#'ish.

public static bool Test<T>(ref A a, out T t)
{
  //...
  else
  {
    if(typeof(T).IsValueType)
    {
      t = (T)(object)0; //Works ONLY if T is valuetype: int, otherwise you get a "must be less than infinity"-error.
    }
    else
    {
      t = (T)(object)null; //Null ref exception in the cast...
    }
    return false;
  }
}

This compiles atleast. But it's still rubbish. Runtime-error galore.
The problem with the value-type is that the object-type remembers what type it really is and when trying to cast to something else... strange things happen (infinity? Really??)
Well this damn well should be doable! So lets be more creative!

public static bool Test<T>(ref A a, out T t)
{
  //...
  else
  {
    if(typeof(T).IsValueType)
    {
      //... still rubbish here
    }
    else
    {
      object o = null;
      t = (T)o; //Tricked you stupid compiler!
    }
    return false;
  }
}

That's right! It looks a stupid insignificant change... But this compiles - and for non-value-types this runs and gives exactly the result we want! If T is a ref-type it gets initilized to null.
Still the problem with value-types. Somewhat reluctantly creativity turns it attention to Reflection. After some random digging around on reflection-stuff, looking for something worth trying out (and no! you cant get the constructor for a value-type, it returns null) I stumbled across a small note on msdn:

"To create an instance of a value type that has no instance
constructors, use the CreateInstance method."

Enter CreateInstance<T>() - http://msdn.microsoft.com/en-us/library/0hcyx2kd.aspx.

"The CreateInstance generic method is used by compilers to implement
the instantiation of types specified by type parameters."

Now we're getting somewhere! Sure it does say

"In general, there is no use for the CreateInstance in application
code, because the type must be known at compile time. If the type is
known at compile time, normal instantiation syntax can be used (new
operator in C#, New in Visual Basic, gcnew in C++)."

But hey - we are not quite doing general stuff, we are in creative-mode, and the compiler is grumpy towards us. Totally justifies giving it a try.

public static bool Test<T>(ref A a, out T t)
{
  //...
  else
  {
    if(typeof(T).IsValueType)
    {
      t = Activator.CreateInstance<T>(); //Probably not your everyday code...
    }
    else
    {
      object o = null;
      t = (T)o;
    }
    return false;
  }
}

AND BAM! That was it! It totally works soo good!
Below is some code thats been tested and run in both VS2010SP1 and MonoDevelop (with Unity3.4)

using System;

namespace GenericUnrestrictedOutParam
{
    class Program
    {
        class TestClass
        {
            public int i;
        }

        struct TestStruct
        {
            int i;
            TestClass thing;
        };

        public static void NullWorkaround<T>(out T anything)
        {
            if (typeof(T).IsValueType)
            {
                anything = Activator.CreateInstance<T>();
            }
            else
            {
                object o = null;
                anything = (T)o;
            }
        }

        static void Main(string[] args)
        {
            int i;
            float f;
            string s;
            TestStruct ts;
            TestClass c;

            NullWorkaround<int>(out i);
            NullWorkaround<float>(out f);
            NullWorkaround<string>(out s);
            NullWorkaround<TestStruct>(out ts);
            NullWorkaround<TestClass>(out c);
        } //Breakpoint here for value-checking
    }
}

And the glorious "output" (from locals-panel @ breakpoint):

        args    {string[0]} string[]
        i   0   int
        f   0.0 float
        s   null    string
-       ts  {GenericUnrestrictedOutParam.Program.TestStruct}    GenericUnrestrictedOutParam.Program.TestStruct
          i 0   int
          thing null    GenericUnrestrictedOutParam.Program.TestClass
        c   null    GenericUnrestrictedOutParam.Program.TestClass

Even the struct with a value and a class-type in it is beautifully handled: value type is 0, class instance is null.
Mission accomplished!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半夏半凉 2024-12-27 18:03:16

您的解决方法似乎没有必要 - 您只需要 default(T) 它适用于引用和值类型:

public static bool Test<T>(ref A a, out T t)
{
  t = default(T);
  return true;
}

Your workaround seems unnecessary - you just need default(T) which works for both reference and value types:

public static bool Test<T>(ref A a, out T t)
{
  t = default(T);
  return true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文