locked
calling an Extension Method from another Extension method RRS feed

  • Question

  • Hi All,

    Does anybody know/has experience calling an extension method from another extension method? Is it even possible at first place?

    Like, I've an ext. method (defined for extending DataContext class) :

    pubic static SaveEntity<T>(this DataContext dc, T Entity)  where T : class
    {
        ....
        ....
    }

    now, the question is : Can I call this method from another ext. method (again defined for extending DataContext class) like :

    public static SaveEntityCollection<T>(this DataContext dc, List<T> Colln)  where T : class
    {
        foreach(T ent in Colln)
        {
            if(ent.IsNew) dc.SaveEntity(?????)
            ....
            ....
            if(ent.IsModified) dc.SaveEntity(?????)
            ....

        }
    }

    ???? = unknown syntax

    I need this type of functionality in one of my projects & want to know if this is a supported language-feature and if so, what could be the syntax for the same.

    Will appreciate any help/link in this regard,

    Thanks in advance,

    Regards,
    Rajesh Moriyani
    Monday, June 9, 2008 10:24 PM

Answers

  • Hi,

    I haven't tried it, but I think you can do it if both methods are in different classes.






    • Marked as answer by jack 321 Thursday, June 12, 2008 8:30 AM
    Monday, June 9, 2008 11:14 PM
  • 1 using System;  
    2 using System.Collections.Generic;  
    3 using System.Linq;  
    4 using System.Text;  
    5 using System.Data.Linq;  
    6  
    7 namespace ExtensionMethods  
    8 {  
    9     public interface IEntity  
    10     {  
    11         bool IsNew { get; set; }  
    12         bool IsModified { get; set; }  
    13     }  
    14  
    15     public class Customer : IEntity  
    16     {  
    17         public bool IsNew {get; set;}  
    18         public bool IsModified {get; set;}  
    19     }  
    20  
    21     public partial class Northwind : DataContext  
    22     {  
    23         public Table<Customer> Customers;  
    24         public Northwind(string connection) : base(connection) { }  
    25     }  
    26  
    27     static class Extensions  
    28     {   
    29         public static void SaveEntity<T>(this DataContext dc, T entity)  
    30         {  
    31             Console.WriteLine(string.Format("{0}({1}) saved", entity.GetType().Name, entity.GetHashCode()));  
    32         }  
    33  
    34         public static void SaveEntityCollection<T>(this DataContext dc, List<T> colln)  
    35         {  
    36             foreach(T ent in colln)  
    37             {  
    38                 if (((IEntity)ent).IsNew || ((IEntity)ent).IsModified)  
    39                 {  
    40                     dc.SaveEntity(ent);  
    41                 }  
    42             }  
    43         }  
    44     }  
    45  
    46     class Program  
    47     {  
    48         static void Main(string[] args)  
    49         {  
    50             List<Customer> myList = new List<Customer>();  
    51  
    52             {  
    53                 myList.Add(new Customer() { IsModified = falseIsNew = true });  
    54                 myList.Add(new Customer() { IsModified = falseIsNew = true });  
    55                 myList.Add(new Customer() { IsModified = falseIsNew = true });  
    56                 myList.Add(new Customer() { IsModified = falseIsNew = true });  
    57                 myList.Add(new Customer() { IsModified = falseIsNew = true });  
    58             };  
    59  
    60             // DataContext takes a connection string.   
    61             DataContext db = new DataContext(@"c:\Northwnd.mdf");  
    62  
    63             db.SaveEntityCollection(myList);  
    64             Console.ReadKey();  
    65         }  
    66     }  
    67 }  
    68  

    Hello Rajesh

     

    Does the above code help you in resolving your query?

     

    Please let me know,

    Sunil Vishwas

     

     

    • Edited by Sam Vishwas Tuesday, June 10, 2008 12:51 AM replaced following with above to show code location
    • Proposed as answer by Sam Vishwas Wednesday, June 11, 2008 12:10 AM
    • Marked as answer by jack 321 Thursday, June 12, 2008 8:30 AM
    Monday, June 9, 2008 11:47 PM
  • Yes: An extension method can call an another extension method: Please find the following example:

     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;  
            }  
        } 


    Thanks,
    Tariq A Karim (http://www.moplah.blogspot.com)
    • Marked as answer by jack 321 Thursday, June 12, 2008 8:30 AM
    Tuesday, June 10, 2008 12:39 AM

All replies

  • Hi,

    I haven't tried it, but I think you can do it if both methods are in different classes.






    • Marked as answer by jack 321 Thursday, June 12, 2008 8:30 AM
    Monday, June 9, 2008 11:14 PM
  • 1 using System;  
    2 using System.Collections.Generic;  
    3 using System.Linq;  
    4 using System.Text;  
    5 using System.Data.Linq;  
    6  
    7 namespace ExtensionMethods  
    8 {  
    9     public interface IEntity  
    10     {  
    11         bool IsNew { get; set; }  
    12         bool IsModified { get; set; }  
    13     }  
    14  
    15     public class Customer : IEntity  
    16     {  
    17         public bool IsNew {get; set;}  
    18         public bool IsModified {get; set;}  
    19     }  
    20  
    21     public partial class Northwind : DataContext  
    22     {  
    23         public Table<Customer> Customers;  
    24         public Northwind(string connection) : base(connection) { }  
    25     }  
    26  
    27     static class Extensions  
    28     {   
    29         public static void SaveEntity<T>(this DataContext dc, T entity)  
    30         {  
    31             Console.WriteLine(string.Format("{0}({1}) saved", entity.GetType().Name, entity.GetHashCode()));  
    32         }  
    33  
    34         public static void SaveEntityCollection<T>(this DataContext dc, List<T> colln)  
    35         {  
    36             foreach(T ent in colln)  
    37             {  
    38                 if (((IEntity)ent).IsNew || ((IEntity)ent).IsModified)  
    39                 {  
    40                     dc.SaveEntity(ent);  
    41                 }  
    42             }  
    43         }  
    44     }  
    45  
    46     class Program  
    47     {  
    48         static void Main(string[] args)  
    49         {  
    50             List<Customer> myList = new List<Customer>();  
    51  
    52             {  
    53                 myList.Add(new Customer() { IsModified = falseIsNew = true });  
    54                 myList.Add(new Customer() { IsModified = falseIsNew = true });  
    55                 myList.Add(new Customer() { IsModified = falseIsNew = true });  
    56                 myList.Add(new Customer() { IsModified = falseIsNew = true });  
    57                 myList.Add(new Customer() { IsModified = falseIsNew = true });  
    58             };  
    59  
    60             // DataContext takes a connection string.   
    61             DataContext db = new DataContext(@"c:\Northwnd.mdf");  
    62  
    63             db.SaveEntityCollection(myList);  
    64             Console.ReadKey();  
    65         }  
    66     }  
    67 }  
    68  

    Hello Rajesh

     

    Does the above code help you in resolving your query?

     

    Please let me know,

    Sunil Vishwas

     

     

    • Edited by Sam Vishwas Tuesday, June 10, 2008 12:51 AM replaced following with above to show code location
    • Proposed as answer by Sam Vishwas Wednesday, June 11, 2008 12:10 AM
    • Marked as answer by jack 321 Thursday, June 12, 2008 8:30 AM
    Monday, June 9, 2008 11:47 PM
  • Yes: An extension method can call an another extension method: Please find the following example:

     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;  
            }  
        } 


    Thanks,
    Tariq A Karim (http://www.moplah.blogspot.com)
    • Marked as answer by jack 321 Thursday, June 12, 2008 8:30 AM
    Tuesday, June 10, 2008 12:39 AM
  • Hi All,

    Thanks one and all (specially Sunil) for reading my post & answering with code-snippets. It's really very helpful.

    Thanking you very much,

    Regards,
    Rajesh Moriyani
    Tuesday, June 10, 2008 9:52 PM