locked
generic method in C# RRS feed

  • Question

  • <!-- /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} @page Section1 {size:8.5in 11.0in; margin:1.0in 1.25in 1.0in 1.25in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-paper-source:0;} div.Section1 {page:Section1;} -->

    Hi,

    I’m trying to write a generic method in C# and I’m getting errors that I don’t understand.

     

    The problem that I’m having is that I get 7 errors stating that “’T’ dose not contain a definition for …”, and it flags both Node.Function and Node.Parameter which are both properties in the user defined types I want to pass.

     

    The reason I don’t understand is, since it’s a generic method, the compiler shouldn’t try to resolve the fields or properties until run time (I would think). But it’s looking for them during design time, is there a way that I can change this behavior, or am I missing something?

     

    I've underlined where I'm getting the errors.

     

    The generic method so far is:

     

    private bool BasicValidationOfCollections<T>(BindingList<T> pList)

            {

                foreach (T Node in pList)

                {

     

                    if (Node.Function == null && Node.Parameter != null )

                    {

                       

                        return false ;

                    }

     

                    if (Node.Function == "ClassName" && Node.Parameter != null )

                    {

                       

                        return false ;

                    }

                    else if (Node.Function != "SimpleTypeName" &&

    Node.Function != null && Node.Parameter == null )

                     {

                       

                        return false ;

                    }

                }

                return true ;

            }

     

    Any suggestions would be appreciated,

    Thanks, BobKey

    Thursday, June 11, 2009 3:16 PM

Answers

  • What you are looking for is a dynamic object, not generic. Let's not get into dynamic details and focus on generic. Generics does the binding at compile time, so it will only let you use members that it knows that T has.

    If you have a base class or interface that has the Function and Parameter properties, you can define T the following way:

    private  bool BasicValidationOfCollections<T>(BindingList<T> pList) where T: IMyInterface

    Hope it helps.

    PS: Dynamic does bind at run-time, but you'll have to wait for C# 4.0 to use it.

    I always try to Keep it Sharp & simple.
    • Marked as answer by Harry Zhu Thursday, June 18, 2009 8:52 AM
    Thursday, June 11, 2009 3:30 PM
  • When you use generics (with no constraints), the compiler will make sure that your generic method (or class) will work with ANY type.  This means that anything you do must be available for any object, whether that's your class, or an "int", or a "double", etc.

    In your case, if somebody called this with a double:  obj.BasicValidationOfCollections<double>(doubleList); - this would not be valid, since "double" doesn't have properties named "Function" and "Parameter".

    You can, however, work around this by adding constraints to your generic function.  This tells the compiler that your function can only be called with specific types of "T".  If, for example, you know that your list will always be of an object derived from an interface (ie: ISomeInterface), you can say:

    private bool BasicValidationOfCollections<T>(BindingList<T> pList) where T : ISomeInterface

     

    This will allow your function to use methods and properties defined on ISomeInterface in your method.  If ISomeInterface exposed properties named "Function" and "Parameter", your method would compile.

     

    You can also use a class as the constraint.  This lets T be any instance of that class or a class derived from that class.  This would provide you the same flexibility.

    In your case, it sounds like you just need to add an interface that your user defined types implement, and constrain to that interface.



    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by Harry Zhu Thursday, June 18, 2009 8:52 AM
    Thursday, June 11, 2009 3:44 PM
    Moderator

All replies

  • What you are looking for is a dynamic object, not generic. Let's not get into dynamic details and focus on generic. Generics does the binding at compile time, so it will only let you use members that it knows that T has.

    If you have a base class or interface that has the Function and Parameter properties, you can define T the following way:

    private  bool BasicValidationOfCollections<T>(BindingList<T> pList) where T: IMyInterface

    Hope it helps.

    PS: Dynamic does bind at run-time, but you'll have to wait for C# 4.0 to use it.

    I always try to Keep it Sharp & simple.
    • Marked as answer by Harry Zhu Thursday, June 18, 2009 8:52 AM
    Thursday, June 11, 2009 3:30 PM
  • When you use generics (with no constraints), the compiler will make sure that your generic method (or class) will work with ANY type.  This means that anything you do must be available for any object, whether that's your class, or an "int", or a "double", etc.

    In your case, if somebody called this with a double:  obj.BasicValidationOfCollections<double>(doubleList); - this would not be valid, since "double" doesn't have properties named "Function" and "Parameter".

    You can, however, work around this by adding constraints to your generic function.  This tells the compiler that your function can only be called with specific types of "T".  If, for example, you know that your list will always be of an object derived from an interface (ie: ISomeInterface), you can say:

    private bool BasicValidationOfCollections<T>(BindingList<T> pList) where T : ISomeInterface

     

    This will allow your function to use methods and properties defined on ISomeInterface in your method.  If ISomeInterface exposed properties named "Function" and "Parameter", your method would compile.

     

    You can also use a class as the constraint.  This lets T be any instance of that class or a class derived from that class.  This would provide you the same flexibility.

    In your case, it sounds like you just need to add an interface that your user defined types implement, and constrain to that interface.



    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by Harry Zhu Thursday, June 18, 2009 8:52 AM
    Thursday, June 11, 2009 3:44 PM
    Moderator