问题
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 calltypeof(someObject)
. You can only callsomeObject.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