Can I access the this object in my extension methods?
So far this is what I have:
void Main() {
IQueryable<DataContextTable> list = DataCont开发者_开发知识库extTables.First().NewMethod(this);
}
public static class ExtensionMethods {
public static IQueryable<DataContextTable> NewMethod(this DataContextTable table, TypedDataContext context) {
return context.DataContextTables.Where(item => item.SomeProperty == true).AsQqueryable();
}
}
as you can see I still need to pass the TypedDataContext as parameters to my extension methods. Is there any other way I can do this?
I created a static member of type TypedDataSet and "initialize" it in the Main() function with this.
void Main() {
ExtensionMethods.Context = this;
IQueryable<DataContextTable> list = DataContextTables.First().NewMethod(this);
}
public static class ExtensionMethods {
public static TypedDataSet Context;
public static IQueryable<DataContextTable> NewMethod(this DataContextTable table) {
return Context.DataContextTables.Where(item => item.SomeProperty == true);
}
}
Similar to acermate433s' answer, but In LINQPad 4 I created a static member of type TypedDataContext:
void Main()
{
MyExtensions.Context = this;
}
public static class MyExtensions
{
public static TypedDataContext Context { get; set; }
// your method here
}
精彩评论