Calling an Extension method from another Extension method (C# 3.0)
-
Is it possible/valid to call an extension(ext.) method from another ext. method? Like I've two ext. methods which extend DataContext class : (1) SaveEntity(this DataContext dc, T Entity) & (2) SaveCollection(this DataContext dc, List Colln) I want to call SaveEntity method from(within) SaveCollection method in a foreach loop like : foreach(T entity in Colln) { if(entity.IsNew) dc.SaveEntity(entity); if(entity.IsModified) dc.SaveEntity(entity); .... } I've tried with various syntax-combinations for calling "SaveEntity" from "SaveCollection" method but end up with compilation-errors. Will appreciate if someone can let me know (or direct to some link) if this is a valid/supported-feature and if yes, what could be the syntax for calling "SaveEntity" method. Thanks for reading this & your replies, Rajesh Moriyani rkmoriyani@gmail.com
-
Is it possible/valid to call an extension(ext.) method from another ext. method? Like I've two ext. methods which extend DataContext class : (1) SaveEntity(this DataContext dc, T Entity) & (2) SaveCollection(this DataContext dc, List Colln) I want to call SaveEntity method from(within) SaveCollection method in a foreach loop like : foreach(T entity in Colln) { if(entity.IsNew) dc.SaveEntity(entity); if(entity.IsModified) dc.SaveEntity(entity); .... } I've tried with various syntax-combinations for calling "SaveEntity" from "SaveCollection" method but end up with compilation-errors. Will appreciate if someone can let me know (or direct to some link) if this is a valid/supported-feature and if yes, what could be the syntax for calling "SaveEntity" method. Thanks for reading this & your replies, Rajesh Moriyani rkmoriyani@gmail.com
Calling extension methods from another is quite possible. I think the problem is that your class is named something like Extensions, which causes the problem. Instead of putting T in the class, put it in the method signature like so: public static void SaveEntity(this DataContext dc, T Entity) where T : whatever Also, is the containing class static? If there are still some compilation errors, please post them.
-
Is it possible/valid to call an extension(ext.) method from another ext. method? Like I've two ext. methods which extend DataContext class : (1) SaveEntity(this DataContext dc, T Entity) & (2) SaveCollection(this DataContext dc, List Colln) I want to call SaveEntity method from(within) SaveCollection method in a foreach loop like : foreach(T entity in Colln) { if(entity.IsNew) dc.SaveEntity(entity); if(entity.IsModified) dc.SaveEntity(entity); .... } I've tried with various syntax-combinations for calling "SaveEntity" from "SaveCollection" method but end up with compilation-errors. Will appreciate if someone can let me know (or direct to some link) if this is a valid/supported-feature and if yes, what could be the syntax for calling "SaveEntity" method. Thanks for reading this & your replies, Rajesh Moriyani rkmoriyani@gmail.com
Yes you can an extension method within inside an extension method. public static class ExtensionClass1 { public static string Info<T> (this T obj) where T:class { return obj.MoreInfo<T>() + ":" + obj.GetType().Name; } } public static class ExtensionClass2 { public static string MoreInfo<T>(this object obj) where T: class { return obj.GetType().Assembly.FullName; } }
Tariq A Karim http://moplah.blogspot.com/