具有泛型类型参数的C#构造函数与接口结合

时间:2018-11-26 20:10:59

标签: c# generics interface

好,我们走了! 我尝试做一件我从未尝试过的事情,然后……C#咬住了我。

问题是我的程序需要将一些输入对象转换为一个输出对象,一次要将许多输入对象转换为一个对象。而out需要in进行实例化。

我不确定在这一点上我是否很清楚...所以一个例子来说明这一点会更好:

public class Class1
{
    public interface ITranslatable { }

    public interface ITranslatable<T> { }

    public class OriginClass : ITranslatable { }

    public class TargetClass : ITranslatable<OriginClass>
    {
        public TargetClass(OriginClass origin)
        {
            // Instantiate some properties from arg
        }
    }

    public class Test
    {
        public Y Execute<X, Y>(X origin, Y target)
            where X : ITranslatable
            where Y : ITranslatable<X>, new()
        {
            target = new Y(origin); // <= How can I make this
            // Some stuff
            return target;
        }
    }

    public TargetClass Function(OriginClass origin, TargetClass target)
    {
        var test = new Test();
        return test.Execute(origin, target);
    }
}

2 个答案:

答案 0 :(得分:0)

您声明了new()约束,这意味着您希望类具有empy无参数构造函数。

下面的代码符合您的需求吗?

 public class Class1
{
    public interface ITranslatable { }

    public interface ITranslatable<T>
    {
        T Origin { get; set; }
    }

    public class OriginClass : ITranslatable
    {

    }

    public class TargetClass : ITranslatable<OriginClass>
    {
        private OriginClass _origin;

        public OriginClass Origin
        {
            get => _origin;
            set
            {
                //Do some stuff
                _origin = value;
            }
        }

        public TargetClass()
        {

        }

    }

    public class Test
    {
        public Y Execute<X, Y>(X origin, Y target)
            where X : ITranslatable
            where Y : ITranslatable<X>, new()
        {
            var result = new Y {Origin = origin};

            // Some stuff
            return target;
        }
    }

    public TargetClass Function(OriginClass origin, TargetClass target)
    {
        var test = new Test();
        return test.Execute(origin, target);
    }
}

答案 1 :(得分:0)

经过几次尝试,我找到了解决方案:使用抽象类。

解决方案:

public class Class1
{
    public interface ITranslatable { }

    public interface ITranslatableOut
    {
        ITranslatable Origin { set; }
    }

    public class OriginClass : ITranslatable
    {
        public string Custom { get; set; }
    }

    public abstract class TargetBase : ITranslatableOut
    {
        public ITranslatable Origin { set { Initialize(value); } }

        protected abstract void Initialize(ITranslatable input);
    }

    public class TargetClass : TargetBase
    {
        protected override void Initialize(ITranslatable input)
        {
            // Initialize some properties
        }
    }

    public class Test
    {
        public Y Execute<X, Y>(X origin, Y target)
            where X : ITranslatable
            where Y : ITranslatableOut, new()
        {
            target = new Y { Origin = origin }; // It works !
            // Some stuff
            return target;
        }
    }

    public TargetClass Function(OriginClass origin, TargetClass target)
    {
        var test = new Test();
        return test.Execute(origin, target);
    }
}