I have a problem about designing my application. I have a webservice with methods. Methods will return a encrypted binary data. Data after decrypting is a list of objects. Object can be Object1, or Object2, or Object3 which depend on called method.
I want to write only one function which return a List of object I want depend on called method. Example:
public static List<TKey> Generate(DataType type)
{
List<TKey> l = new List<TKey>();
switch (type)
{
case DataType.String:
byte[] data = Decrypt(MyWebService.GetData1());
l = (List<TKey>)data;
case DataType.Int:
byte[] data = Decrypt(MyWebService.GetData1());
l = (List<TKey>)data;
}
return l;
}
Note: this function has error when building. I give this function so that you can understand my purpose.
My Purpose is here:
List<string> myString = Generate(DataType.String);
List<int> myInt = Generate(DataType.Int);
Thanks.
You are most likely looking for a generic method:
You also will need a type specific deserialization method (
Deserializein the example), which would do the actual work of converting an arbitrary byte array into a list of a specific type – since it is entirely up to you how that mapping should work (the framework can’t know really) you will have to implement it yourself.