I'm having trouble finding the right solution for what I am trying to achieve. The closest thing I can come up with is to set the child classes with object and casting (which is ugly to me).
I basically want to define basic domain objects that work with a specific type of data, that differs per class. The code below will not compile and I know this, I'm trying to show you want
I want to achieve. It's pretty basic until the Main function.
public abstract class WorkerTypeBase<T>
{
// other methods and properties here
abstract T GetResult();
abstract void SetResult(T value);
}
public ChildWorkerClass1 : WorkerTypeBase<ResultType1>
{
private ResultType1 _resType1;
public override ResultType1 GetResult()
{ return _resType1; }
public override void SetResult(ResultType1 value) { _resType = value; }
}
public ChildWorkerClass2 : WorkerTypeBase<string>
{
private string _resType1;
public override string GetResult()
{ return _resType1; }
public override void SetResult(string value) { _resType = value; }
}
static int Main()
{
Dictionary<string, WorkerTypeBase<>> myDictionary = new Dictionary<string, WorkerTypeBase<>>();
WorkerTypeBase def = _factory.GetRandomWorkerBaseType("<Param makes factory return ChildWorkerClass2>"); // This will not work, you have to specify the <T> at compile time. I don't want to have to care what T is at this point in time.
myDictionary.Add("1001", def);
Console.Writeline("Result: {0}", (def as WorkerTypeBase<string>).GetResult());
WorkerTypeBase def2 = _factory.GetRandomWorkerBaseType("<Param makes factory return ChildWorkerClass1>");
myDictionary.Add("1002", def2);
Console.Writeline("Result: {0}", (def2 as WorkerTypeBase<ResultType1>).GetResult().CustomString);
}
I don't want to have to care about what T is unless I specifically want to work with the data that object would be holding. The only other thing I can think of is attaching an interface and then casting per child class, but that is an even uglier version of the object casting solution. I'm not sure if a dynamic keyword option is available or not. Maybe this isn't even possible and I'm making it too complicated, but it seems like there is something I'm missing here.
Question
sathenzar
I'm having trouble finding the right solution for what I am trying to achieve. The closest thing I can come up with is to set the child classes with object and casting (which is ugly to me).
I basically want to define basic domain objects that work with a specific type of data, that differs per class. The code below will not compile and I know this, I'm trying to show you want
I want to achieve. It's pretty basic until the Main function.
I don't want to have to care about what T is unless I specifically want to work with the data that object would be holding. The only other thing I can think of is attaching an interface and then casting per child class, but that is an even uglier version of the object casting solution. I'm not sure if a dynamic keyword option is available or not. Maybe this isn't even possible and I'm making it too complicated, but it seems like there is something I'm missing here.
Link to comment
Share on other sites
4 answers to this question
Recommended Posts