Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Calling an Extension method from another Extension method (C# 3.0)

Calling an Extension method from another Extension method (C# 3.0)

Scheduled Pinned Locked Moved C#
csharpcomquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rajesh Moriyani
    wrote on last edited by
    #1

    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

    J T 2 Replies Last reply
    0
    • R Rajesh Moriyani

      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

      J Offline
      J Offline
      jamie550
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • R Rajesh Moriyani

        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

        T Offline
        T Offline
        Tariq A Karim
        wrote on last edited by
        #3

        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/

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups