How to access a method on a generic datacontext which is only created at runtime
Posted
by Jeremy Holt
on Stack Overflow
See other posts from Stack Overflow
or by Jeremy Holt
Published on 2010-04-16T01:23:17Z
Indexed on
2010/04/16
1:33 UTC
Read the original article
Hit count: 403
I'm creating my generic DataContext using only the connectionString in the ctor. I have no issues in retrieving the table using DataContext.GetTable(). However, I need to also be able to retrieve entities of inline table functions. The dbml designer generates
public IQueryable<testFunctionResult> testFunction()
{
return this.CreateMethodCallQuery<testFunctionResult>(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())));
}
The question is how do I get the MethodInfo.GetCurrentMethod() when the DataContext has no method called "testFunction", i.e.typeof(DataContext).GetMethod("testFunction") returns null?
What I'm trying to achieve is something like:
public class UnitofWork<T>
{
public UnitofWork(string connectionString)
{
this.DataContext = new DataContext(connectionString);
}
public UnitofWork(IQueryable<T> tableEntity)
{
_tableEntity = tableEntity;
}
public IQueryable<T> TableEntity
{
get
{
if (DataContext == null)
return _tableEntity;
var metaType = DataContext.Mapping.GetMetaType(typeof (T));
if (metaType.IsEntity)
_tableEntity = DataContext.GetTable<T>();
else
{
var s = typeof(T).Name;
string methodName = s.Substring(0, s.IndexOf("Result")) + "()"; // the designer automatically affixes "Result" to the type name
// Make a method from methodName
// _tableEntity = DataContext.CreateMethodCallQuery(DataContext, method, new object[]{});
}
return _tableEntity;
}
set { _tableEntity = value; }
}
)
Thanks in advance for any insight
Jeremy
© Stack Overflow or respective owner