المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Implementation of Generic Method in List



C# Programming
06-27-2012, 04:12 AM
Hi guys!
I'm dealing with a problem I can't find a solution for. What I'm trying to do is this:
I have a Model which should contain a generic method (generic input and return value).
And furthermore these Models should be merged into a List.
And here it gets complicated and I end up in a dead end.

Here's the code which hopefully makes it more clear:
class MainClass { public static void Main (string[] args) { List containers = new List(); containers.Add(new ContainerA()); containers.Add(new ContainerB()); } } abstract class ContainerBase { public string Name { get; set; } public abstract TResult Calculate(params TInput[] input); } class ContainerA : ContainerBase { #region implemented abstract members of Generics.ContainerBase public override TResult Calculate (params TInput[] input) // !! public override double Calculate(params int[] input) { int[] ints = input as int[]; double result = 0; return result; // !! throws an error return result as TResult; // !! doesn't work either } #endregion } class ContainerB : ContainerBase { #region implemented abstract members of Generics.ContainerBase public override TResult Calculate (params TInput[] input) // !! public override uint Calculate(params uint[] input) { throw new System.NotImplementedException (); } #endregion }
This is needed because I want to be able to combine different algorithms (presented by the Model), e.g. Algo 1: combine 3 integers, Algo 2: divide two integers, Algo 3: get the sort via Drag&Drop (in MVVM - hence the unified approach).

An Alternative would be to store a whole Metod in a Property in the Model, but it needs to be generic as well. And I have no idea how to do that either.

Help would be appreciated!