简体   繁体   English

铸造类型参数

[英]Casting type parameter

The title might be a bit vague, but what I want is to achieve this, but then without using the extra lambda in the create method:标题可能有点含糊,但我想要的是实现这一点,然后在创建方法中不使用额外的 lambda :

public class Wrapper
{
    public static Wrapper Create<T>(Func<T, bool> someFunc) where T : Stream
    {
        return new Wrapper(a => someFunc(a as T)); // Works, but lambda
        return new Wrapper((Func<Stream, bool>)(object)someFunc); // Runtime error
        return new Wrapper((Func<Stream, bool>)someFunc); // Compile error
    }

    Func<Stream, bool> _someFunc;
    private Wrapper(Func<Stream, bool> someFunc)
    {
        _someFunc = someFunc;
    }
}

Is it possible to do this, if so, how?是否有可能做到这一点,如果可以,怎么做?

EDIT: The stream class is just as example.编辑: stream class 就是一个例子。

In my code I won't be using a stream class, and as for why the type-parameter, because it's returned from a generic method, which is an extension method.在我的代码中,我不会使用 stream class,以及为什么使用类型参数,因为它是从泛型方法返回的,这是一种扩展方法。

Also, the call will always be type-safe, I wanted to make the 'Wrapper' class itself generic, but then it'd be unable to add it to generic collections, because they'd have different type-parameters.此外,调用将始终是类型安全的,我想让“包装器”class 本身具有通用性,但随后无法将其添加到通用 collections 中,因为它们具有不同的类型参数。

There's no way to do what you want without a lambda.如果没有 lambda,就无法做你想做的事。 A Func<Stream, bool> must accept any type of Stream as a parameter, so a method that takes a T (which is a specific kind of Stream ) is not compatible with the signature of Func<Stream, bool> Func<Stream, bool>必须接受任何类型的Stream作为参数,因此采用T (这是Stream的特定类型)的方法与Func<Stream, bool>的签名不兼容

Anyway, in the code you posted the generic parameter is completely useless;无论如何,在您发布的代码中,泛型参数完全没用; so just remove it, and make the method non-generic.所以只需将其删除,并使该方法非通用。

This can't be casted (even with contravariance support) because it is not a safe cast.这不能被铸造(即使有逆变支持),因为它不是一个安全的铸造。

If I have a function that acts on FileStream, then try to pass it into Wrapper, I've passed it in as a function that claims it can act on any Stream.如果我有一个作用于 FileStream 的 function,然后尝试将其传递给 Wrapper,我已将其作为 function 传递,声称它可以作用于任何ZEAE835E83C0494A376229F254F7。 If I then invoke that function from within Wrapper using a MemoryStream, for instance, then the delegate binding fails.例如,如果我随后使用 MemoryStream 从 Wrapper 中调用 function,则委托绑定将失败。 The Func<Stream, bool> is not really a Func<Stream, bool> at all. Func<Stream, bool>根本不是真正的Func<Stream, bool>


if you want to make Wrapper generic ( Wrapper<T> ), you might consider exposing a non-generic interface IWrapper implemented by the generic.如果你想让 Wrapper 泛型( Wrapper<T> ),你可以考虑公开一个由泛型实现的非泛型接口IWrapper This way you can create a collection of IWrapper instances and expose the functionality that is valid to perform on any Wrapper<T> , while still maintaining type-safety inside each instance.这样,您可以创建IWrapper实例的集合并公开可在任何Wrapper<T>上执行的有效功能,同时仍保持每个实例内部的类型安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM