English 中文(简体)
我如何解决一个通用类别<T>的设想?
原标题:How can I resolve a generic class <T> scenario?

I ve got problem using generals. I m 创建一个称为IProblem的接口,每个问题都有结果(回答)和结果(如果正确的话)

public interface IProblem<T>
{
    ushort ResultCount { get; }
    T[] Results { get; }

    bool IsCorrect();
}

public abstract class ProblemBase<T> : IProblem<T>
{
    private T[] _results;
    private ushort? _resultCount;

    public ushort ResultCount
    {
        get
        {
            if (_resultCount == null) throw new ArgumentNullException("_resultCount");
            return (ushort)_resultCount;
        }
        protected set
        {
            if (_resultCount != value)
                _resultCount = value;
        }
    }

    public T[] Results
    {
        get
        {
            if (_results == null)
                _results = new T[ResultCount];

            return _results;
        }
    }

    public abstract bool IsCorrect();
}

This is an example where I create an arithmetic problem, called ProblemA. T is decimal because the array datatype should be decimal (anothers problems maybe might have string, or int)

public class ProblemA: ProblemBase<decimal>
{
    private decimal _number1;
    private decimal _number2;
    private Operators _operator;

    public decimal Number1
    {
        get { return _number1; }
        set { _number1 = value; }
    }

    public decimal Number2
    {
        get { return _number2; }
        set { _number2 = value; }
    }

    public Operators Operator
    {
        get { return _operator; }
        set { _operator = value; }
    }

    public decimal Result
    {
        get { return Results[0]; }
        set { Results[0] = value; }
    }

    public ProblemA()
    {
        this.ResultCount = 1;
    }

    public override bool IsCorrect()
    {
        bool result;

        switch (_operator)
        {
            case Operators.Addition:
                result = this.Result == (this.Number1 + this.Number2);
                break;
            case Operators.Subtract:
                result = this.Result == (this.Number1 - this.Number2);
                break;
            case Operators.Multiplication:
                result = this.Result == (this.Number1 * this.Number2);
                break;
            case Operators.Division:
                result = this.Result == (this.Number1 / this.Number2);
                break;
            default:
                throw new ArgumentException("_operator");
        }

        return result;
    }
}

I m using MVVM, so I d like to have a ViewModel for each problem where contains ProblemBase<T> as property, but how it s a generic, I guess it will be a problem if a put in IProblemViewModel as generic.

public interface IProblemViewModel : IViewModel
{
    ProblemBase<T> Problem { get; set; }
}

I said this because later a plan to use a ObservableCollection<IProblemViewModel>, so I m not sure if there s no problem if I write IProblemViewModel or IProblemViewModel<T>. Thanks in advance.

最佳回答

也许我没有很好地理解这一点,但这是你之后的吗?

    ObservableCollection<IProblemViewModel<object>> collection = new ObservableCollection<IProblemViewModel<object>>
    {
        new ProblemViewModel<DerivedResult>(),
        new ProblemViewModel<OtherResult>()
    };

可以通过宣布通用论点为共犯来实现。

你们还可以改变收集工作,以利收集工作。

ObservableCollection<IProblem<BaseType>>

and just have it accept a specific result chain. In this example, DerivedResult and OtherResult must then inherit from BaseType to fit into the collection.

主要的告诫是,原始类型丝毫不适应这种等级。 页: 1

Of course, you could implement a simple carrier, for example Boxer which would box any value type instead of implementing one for each type.

最后一次是: 不可能在var类上拥有一套财产,例如<代码>IProblemViewModel。 只能支持<条码>。

一个完整的、可汇编的例子:

class Program
{

    public interface IProblem<out T>
    {
        ushort ResultCount { get; }
        T[] Results { get; }

        bool IsCorrect();
    }

    public class ProblemBase<T> : IProblem<T>
    {
        private T[] _results;
        private ushort? _resultCount;

        public ushort ResultCount
        {
            get
            {
                if (_resultCount == null) throw new ArgumentNullException("_resultCount");
                return (ushort)_resultCount;
            }
            protected set
            {
                if (_resultCount != value)
                    _resultCount = value;
            }
        }

        public T[] Results
        {
            get
            {
                if (_results == null)
                    _results = new T[ResultCount];

                return _results;
            }
        }

        public bool IsCorrect()
        {
            return true;
        }
    }

    public interface IProblemViewModel<out T>
    {
        IProblem<T> Problem { get; }
    }

    public class BaseResult
    {

    }

    public class DerivedResult : BaseResult
    {

    }

    public class OtherResult : BaseResult
    {

    }

    public class ProblemViewModel<T> : IProblemViewModel<T>
    {

        public IProblem<T> Problem
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
    }


    static void Main(string[] args)
    {
        ObservableCollection<IProblemViewModel<object>> collection = new ObservableCollection<IProblemViewModel<object>>
        {
            new ProblemViewModel<DerivedResult>(),
            new ProblemViewModel<OtherResult>()
            //, new ProblemViewModel<int>()   // This is not possible, does not compile.
        };
    }
}
问题回答

您的看法模式接口可以这样界定:

public interface IProblemViewModel<T> : IViewModel
{
    //No reason to use the base here instead of the interface
    IProblem<T> Problem { get; set; }
}

我不敢肯定,你是否正在计划把这个问题与世界森林论坛或银星的一个接口联系起来,但如果你确保问题也能够执行。 对那些不实施公证用途的物体,对非属性财产进行约束,造成记忆泄露,永远不会释放目标。 http://support.microsoft.com/kb/938416“rel=“nofollow”http://support.microsoft.com/kb/938416。

EDIT: 补充对评论的答复。

请更正在<代码> 缩略语 如果您打算展示一种以上类型的<代码><T>。 但是,既然你具有约束力,那么实际上就没有什么目标类型是你对其有约束力的,为什么不仅仅将收集列入<代码>。 ObservableCollection<IViewModel>?





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

热门标签