本文介绍了如何从会议未知类型和ID获取对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要做的就是从会话获取对象数据。
What I'm trying to do is get object data from session.
code以下应全部清除:
Code below should make clear all:
//Type of object which i need to get in runtime
Type someUnknownType = typeof(someObject);
int id = 1; // for example
// here i got error cannot resolved someUnknownType
ISession.Get<someUnknownType>(id);
我怎么能这样做?
How can I do that?
编辑: someObject
是实例变量。
推荐答案
首先,你有一个错误:
- 如果
someObject
是一个实例变量,你不能叫的typeof(someObject)
。你只能叫someObject.GetType()
- 如果它是一个类型,你仍然可以使用通用版本。
- if
someObject
is an instance variable, you can't calltypeof(someObject)
. You can only callsomeObject.GetType()
- if it is a type, you still use the generic version.
您是否尝试过使用非通用版本:
object x = ISession.Get(someObject.GetType(), id);
这篇关于如何从会议未知类型和ID获取对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!