Cast with GetType()

后端 未结 5 1901
无人及你
无人及你 2020-12-15 03:13

Is it possible to cast an object to the type returned from GetType()? I\'d like a generic method that can accept an object (for anonymous types) but then return

5条回答
  •  时光取名叫无心
    2020-12-15 03:49

    You can use the Activator.CreateInstance method to create an instance from a type.

    FYI reflection is SLOOWWWW, so if you need to do this cast many times in a row, it may be better to define your types in an enum or something like this then create instances without using reflection.

提交回复
热议问题