How to get object from session with unknown type and id?

冷暖自知 提交于 2020-01-24 20:19:26

问题


What I'm trying to do is get object data from session.

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?

Edit: someObject is instance variable.


回答1:


First, you have a mistake:

  • if someObject is an instance variable, you can't call typeof(someObject). You can only call someObject.GetType()
  • if it is a type, you still use the generic version.

Have you tried to use non-generic version:

object x = ISession.Get(someObject.GetType(), id);



回答2:


You need to use this Get method:

ISession.Get(Type, Object);

Here is an example from the NHibernate documentation:

Cat cat = (Cat) sess.Get(catInstance.GetType(), id);


来源:https://stackoverflow.com/questions/10173254/how-to-get-object-from-session-with-unknown-type-and-id

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!