I have the following class layout:
abstract class GenericClass<TArgs> where TArgs : ArgsBaseClass { }
abstract class ArgsBaseClass { }
class RandomArgs : ArgsBaseClass { }
class RandomClass : GenericClass<RandomArgs> { }
Now I want to be able to create an instance of RandomClass
from an instance of RandomArgs
, without knowing that it should be RandomClass
. I.e. somehow I need to derive the fact that RandomClass
implements GenericClass<RandomArgs>
.
I'm able to create an instance of GenericClass<RandomArgs>
using:
void CreateSpecificInstance(ArgsBaseClass args)
{
Type genericType = typeof(GenericClass<>).MakeGenericType(typeof(args));
var genericInstance = Activator.CreateInstance(genericType, typeof(args));
// But then I need help for the following step:
Type specificType = ... // in this case RandomClass, but should be derived from 'args'.
var specificInstance = (specificType)genericInstance;
}
Any help is appreciated.
Aucun commentaire:
Enregistrer un commentaire