How to use Activator to create an instance of a generic Type and casting it back to that type?

后端 未结 5 635
情歌与酒
情歌与酒 2020-12-01 07:49

I have a generic type Store and use Activator to make an instance of this type. Now how, after using the Activator, can I cast the resulte

5条回答
  •  甜味超标
    2020-12-01 08:05

    Let's say that someObjectThatImplementsIStorable is of type MyStorable.

    e.g. MyStorable someObjectThatImplementsIStorable = new MyStorable( ); ... // rest of your code here.

    Then x cannot be cast to Store, but it can be cast to Store. The following will work: (Store)x

    Note that although MyStorable implements IStorable, there is no relationship between Store and Store. These are two distinct classes that do not derive from each other.

    u.

提交回复
热议问题