English 中文(简体)
通用时间
原标题:Compile time generics syntax
  • 时间:2011-04-13 13:37:49
  •  标签:
  • c#
  • generics
class GenericWrapper<T>
{   
}

class WrapperInstance : GenericWrapper<string>
{   
}

class Usage
{
    public static Usage Create<T1, T2> (T2 t2) where T1 : GenericWrapper<T2>
    {
        return null;
    }
}

...

// works
Usage.Create<WrapperInstance, string>("bar");

// doesnt work
Usage.Create<WrapperInstance>("bar");

我怀疑答案是没有的,但我是否能够把最后一行汇编起来?

我希望汇编者迫使我提出直截了当的论点,而不必知道或首先去审查<条码>《WrapperInstance,看<条码>>>。

我知道,我可以通过使用第一种方法或以<条码>object作为论据和进行操作时间检查来汇编,但这不是问题;] 我基本上怀疑这些是我唯一的两个选择。

增 编

最佳回答

我怀疑答案是没有的,但我是否能够把最后一行汇编起来?

<代码>Create有两个通用参数。 你们要么没有具体说明,要么就明确了。 如果没有,编辑将试图从援引理由中推断出这些类型。 然而,在这种情况下,它不能因为<代码>T1从来不出现在论点清单中。 因此,你必须明确这两点。

问题回答

There are two problems here:

  • You want to infer just one type argument, and specify the other. You can t do that with normal type inference. However, you could make Usage generic, thus specifying one type argument there, and letting the other be inferred using the generic method:

    Usage<WrapperInstance>.Create("foo");
    

    这是我以前经常做的事,但这只是导致第二个问题......

  • The type parameter you want to specify (T1) is constrained by the one you want to infer (T2). The above example can t do that, as Usage<WrapperInstance> doesn t "have" a T2 to validate... and you can t constrain an existing type parameter on a generic method - only ones which are introduced in the method.

我认为,我们有一种方式: 为此:

public class Usage
{
    public static Usage<T2> For<T2>(T2 t2)
    {
        return new Usage<T2>(t2);
    }
}

public class Usage<T2>
{
    private readonly T2 t2; // Assuming we need it

    public Usage(T2 t2)
    {
        this.t2 = t2;
    }

    // I don t know what return type you really want here
    public static Foo Create<T1>() where T1 : GenericWrapper<T2>
    {
        // Whatever
    }
}

你们也这样做:

Usage.Foo("bar").Create<WrapperInstance>();

在不了解你试图做些什么的情况下,我不知道这是否有用——但does <>/em>设法完成你在以下各方面的工作:

  • Validating the WrapperInstance type argument
  • Inferring the string type argument




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...