English 中文(简体)
• 如何创建一种采用T或IEvidable<T>的通用类型?
原标题:How to create a generic type that takes either T or IEnumerable<T>?
  • 时间:2010-03-16 15:21:40
  •  标签:
  • c#
  • generics

我要说的是:

public class Transformer<T, TResult>
    where T : IMessage
    where TResult : IMessage
{
    private Func<T, IEnumerable<TResult>> _transformer;

    public Transformer(Func<T, TResult> transformer)
    {
        _transformer = null // ?
    }

    public Transformer(Func<T, IEnumerable<TResult>> transformer)
    {
        _transformer = transformer;
    }
}

因此,从本质上讲,我要将<代码>Func<T, TResult>改为 in the first Constructionor.

I ve试图创建一种私人内部班级,该班采用Func<T, TResult>,并界定了一种能够使IE获得如此数字的方法:

private class TransformerWrapper
{
    private readonly Func<T, TResult> _func;

    public TransformerWrapper(Func<T, TResult> func)
    {
        _func = func;
    }

    public IEnumerable<TResult> Transform<T>(T message) where T : IMessage
    {
        yield return _func(message);
    }
}

但它并未真正发挥作用。 我犯了一个错误,说代表有了一个无效的论点——不能从T改为T。

首先,汇编者错误发生什么,其次,又是另一种方式?

<>Update>

会后记录 我在座,找到了解决办法:

public Transformer(Func<T, TResult> transformer)
{
    _transformer = new TransformerWrapper<T, TResult>(transformer).Transform;
}

而且,

private class TransformerWrapper<T, TResult>
{
    private readonly Func<T, TResult> _func;

    public TransformerWrapper(Func<T, TResult> func)
    {
        _func = func;
    }

    public IEnumerable<TResult> Transform(T message)
    {
        yield return _func(message);
    }
}

我仍然可以谈谈第一个解决办法为什么不起作用。 我需要考虑这一点......

最佳回答

为此:

public Transformer(Func<T, TResult> transformer)
{
    _transformer = t => Convert(t, transformer);
}

private static IEnumerable<TResult> Convert(T value, Func<T, TResult> transformer)
{
    yield return transformer(t);
}
问题回答

请在<代码>Transform<T>中重新指定新的通用类别<代码>。 如TTResult已在母类中界定,因此没有必要将儿童类别的任何内容列为通用类别。

删除<代码><T>和通用限定词,从方法签名中删除,并汇编。

public IEnumerable<TResult> Transform(T message)
{
    yield return _func(message);
}

3. 改变你的内层:

    private class TransformerWrapper
    {
        private readonly Func<T, TResult> _func;

        public TransformerWrapper(Func<T, TResult> func)
        {
            _func = func;
        }

        public IEnumerable<TResult> Transform(T message)
        {
            yield return _func(message);
        }
    }
}

Comp已经知道什么是T,你不需要再次限制这种方法。





相关问题
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. ...

热门标签