dimanche 10 mai 2015

Using Activator.CreateInstance() with properties

I have the following property in a base (abstract) class:

protected abstract Type TheType { get; }

The above property is instantiated by child classes:

protected override Type TheType
{
  get { return typeof (ChildClass); }
}

I'd like to instantiate the object in the base class:

var obj = (TheType) Activator.CreateInstance<TheType>();

Unfortunately, I get the following compilation error:

Error   1   'BaseClass.TheType' is a 'property' but is used like a 'type'

How may I invoke Activator.CreateInstance() in this scenario?

PS: I've tried changing the property to a field:

protected Type TheType;

Still I get compilation error:

'BaseClass.TheType' is a 'field' but is used like a 'type'





Aucun commentaire:

Enregistrer un commentaire