Reputation: 23
This is my first question so be gentle :)
I want to be able to use method for generic types which will return me object for later use. I have tried the code below but it's not complete.
I need this to call like this: City city = GetDataById(Id);
Of course instead of city I need generic use.
Thanks and regards.
private T GetDataById(Guid Id)
{
T obj;
using (ISession session = Session.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
obj = session.Get<T>(Id);
tx.Commit();
}
return obj;
}
}
Upvotes: 1
Views: 127
Reputation: 124622
You never define T. You... need to.
private T GetDataById<T>(Guid Id) { /* ... */ }
And...
City city = GetDataById(Id);
You'll never be able to call it like that because the type argument cannot be inferred. You will need to supply it, i.e.,
City city = GetDataById<City>(Id);
Upvotes: 2
Reputation: 44921
I think that you want:
private T GetDataById<T>(Guid Id)
{
T obj;
using (ISession session = Session.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
obj = session.Get<T>(Id);
tx.Commit();
}
return obj;
}
}
then call it as:
City city = GetDataById<City>(Id);
Upvotes: 5