简体   繁体   English

C#泛型转换问题

[英]C# generics casting issue

I have a generic Filter class, and I want to make a conditional version of this class: 我有一个通用的Filter类,并且想要创建该类的条件版本:

public abstract class Filter<T, R> 
{
    protected abstract R GetResult(T input);

    private class JoinedFilterIf<S> : Filter<T, S>
    {
        private readonly Filter<T, R> _left;
        private readonly Filter<R, S> _right;
        private readonly Func<R, bool> _condition;

        public JoinedFilterIf(Filter<T, R> left, Filter<R, S> right, Func<R, bool> condition)
        {
            _left = left;
            _right = right;
            _condition = condition;
        }

        protected override S GetResult(T input)
        {
            var result = _left.GetResult(input);
            return _condition(result) ? _right.GetResult(result) : (S)((object)result);
        }
    }
}

Is there any way to avoid boxing in the GetResult return expression? 有什么方法可以避免在GetResult返回表达式中装箱吗?

This seems to compile.. 这似乎可以编译。

What I've added is a where S: R If that's what You were looking for 我添加的是where S: R如果您正在寻找的是

 public abstract class Filter<T, R>
    {
        protected abstract R GetResult(T input);

        private class JoinedFilterIf<S> : Filter<T, S> where S : R
        {
            private readonly Filter<T, R> _left;
            private readonly Filter<R, S> _right;
            private readonly Func<R, bool> _condition;

            public JoinedFilterIf(Filter<T, R> left, Filter<R, S> right, Func<R, bool> condition)
            {
                _left = left;
                _right = right;
                _condition = condition;
            }

            protected override S GetResult(T input)
            {
                var result = _left.GetResult(input);
                return _condition(result) ? _right.GetResult(result) : (S)(result);
            }
        }
    }

Basically what this means - is that it's required that 'S' will be inherited from 'R' 基本上,这意味着-要求'S'必须继承自'R'

The question for me - is why are You introducing an additional type S ? 对我来说,问题是-为什么要引入其他类型S? I believe that in both cases having only 'T' and 'R' defined should be enough.. and the it would look like this 我认为,在两种情况下,仅定义“ T”和“ R”就足够了。看起来像这样

public abstract class Filter<T, R> where R : T
{
    protected abstract R GetResult(T input);

    private class JoinedFilterIf<S> : Filter<T, R> 
    {
        private readonly Filter<T, R> _left;
        private readonly Filter<T, R> _right;
        private readonly Func<R, bool> _condition;

        public JoinedFilterIf(Filter<T, R> left, Filter<T, R> right, Func<R, bool> condition)
        {
            _left = left;
            _right = right;
            _condition = condition;
        }

        protected override R GetResult(T input)
        {
            var result = _left.GetResult(input);
            return _condition(result) ? _right.GetResult(result) : (result);
        }
    }
}

What this is saying - is that the result R will be a derived type from T, so it could be passed back into the 'GetResult' function as if it was of type T 这就是说-结果R将是T的派生类型,因此可以将其传递回'GetResult'函数,就像它是T类型一样。

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

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