Make a non-generic base class and inherit the generic class from it (they can have the same name and only differ by the generic type). On the non-generic base class, create generic instances in your static method (which returns the non-generic base class as result). You can then cast this to the generic type you expect public abstract class Parameter { public static Parameter FromXml(XElement xElement) { ... } public string Name { get; set; } public abstract XElement ToXml(); } public class Parameter: Parameter { public T Value { get; set; } public override XElement ToXml() { ... } } There isn't really another way of doing this, since in your sample the type for T is specified before the static method runs.
Make a non-generic base class and inherit the generic class from it (they can have the same name and only differ by the generic type). On the non-generic base class, create generic instances in your static method (which returns the non-generic base class as result). You can then cast this to the generic type you expect.
Public abstract class Parameter { public static Parameter FromXml(XElement xElement) { ... } public string Name { get; set; } public abstract XElement ToXml(); } public class Parameter: Parameter { public T Value { get; set; } public override XElement ToXml() { ... } } There isn't really another way of doing this, since in your sample the type for T is specified before the static method runs.
Thank you! I thought about that. Are you sure that there is no other way, given that I'm using .
NET 4.0? – Bashir Magomedov Apr 12 at 16:37 Yes, because this would violate the contract, no matter which version you use: in your code the Parameter.FromXml() must return a Parameter since that's the declared return type for this method. – Lucero Apr 12 at 16:39.
This does work for custom classes but it may not work for value types. Take a look at This Post to see how this work for heap types.
As Indicated by SLaks, this is impossible. Think of it this way, Parameter value = Parameter. FromXml(...); How will you ever resolve what the questionmarks should be when it depends on the content of the XmlElement?
You should use a common class which both captures Value as an object and exposes type information.
The problem is: T is evaluated at compile time. The compiler does not have any chance to know, to which actual type your 'sType' will evaluate at runtime. Also, in the compiled assembly, no generics exist anymore.
Therefore, in order to construct a type at runtime, based on condition which can only get evaluated at runtime, you will have to pesent the compiler a concrete type for compiling anyway. So somewhere you may decide your return type in an if .. else cascade like that: if (sType is String) return new Parameter() else if (sType is double) return new Parameter() ... This may be placed inside a factory class or right near to the XElement class, I guess. In order to be able to return Parameter for example, you may use an interface (or a common base class, like in another answer), which may stand for all Parameter variants.
This is the reason, why the method, which parses the Xml IMO is better placed outside of Parameter. Also, since it seems, you are cert of 'deserializing' from XML, consider using some de-/serializer functionality ;).
Also, in the compiled assembly, no generics exist anymore. " - I think your are mixing up Java's erasures with . NET's (real) generics.In .
NET, each generic type forms a distinct type for each combination of generic types used as type arguments. You can create those at runtime using the MakeGenericType() on the open generic Type (e.g.In this sample typeof(Parameter). MakeGenericType(typeof(string)) would yield the same type at runtime as typeof(Parameter)).
– Lucero Apr 13 at 8:42.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.