Answer the question
In order to leave comments, you need to log in
Is it possible to create an inherited class from a static method of an ancestor class?
Is it possible to describe the static method CreateFromBaseObject in the MyBaseClass class so that when this method is called from a child class of MyDerivedClass, an instance of MyDerivedClass is created, but the U parameter is not used?
(Of course, without the implementation of the CreateFromBaseObject method inside MyDerivedClass)
public abstract class MyBaseClass<T>
where T : class, new()
{
protected T RealObject = new T();
public void setRealObject(dynamic realObject) { this.RealObject = realObject; }
public static U CreateFromBaseObject<U>(T baseObject) where U : MyBaseClass<T>, new()
{
var t = new U();
t.setRealObject(baseObject);
return t;
}
}
...
public class MyDerivedClass : MyBaseClass<RealDerivedClass>{...}
...
RealDerivedClass realDerivedClass = new RealDerivedClass();
//Приходится так:
MyDerivedClass myRealDerivedClass = MyDerivedClass.CreateFromBaseObject<MyDerivedClass>(realDerivedClass);
//А хочется так:
MyDerivedClass myRealDerivedClass = MyDerivedClass.CreateFromBaseObject(realDerivedClass);
Answer the question
In order to leave comments, you need to log in
What if so:
using System;
namespace answer
{
using System;
public abstract class MyBaseClass<T, T2>
where T : class, new()
where T2: class, new()
{
protected T RealObject = new T();
public void setRealObject(dynamic realObject) { this.RealObject = realObject; }
public static U CreateFromBaseObject<U>(T baseObject) where U : MyBaseClass<T>, new()
{
var t = new U(); t.setRealObject(baseObject); return t;
}
public static T2 CreateFromBaseObject(T baseObject)
{
return CreateFromBaseObject<T2>(baseObject);
}
}
public class RealDerivedClass {}
public class MyDerivedClass : MyBaseClass<RealDerivedClass, MyDerivedClass>{}
public class Test
{
public static void Main()
{
RealDerivedClass realDerivedClass = new RealDerivedClass();
//Приходится так:
MyDerivedClass myRealDerivedClass = MyDerivedClass.CreateFromBaseObject<MyDerivedClass>(realDerivedClass);
//А хочется так:
MyDerivedClass myRealDerivedClass2 = MyDerivedClass.CreateFromBaseObject(realDerivedClass);
}
}
}
as an option to use reflection via caller member name
example for INotifyPropertyCahnged inotifypropertychanged without the backing fields of the msdn
dock
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question