ObjectDataSource with Generic Class

Discussion in 'ASP .Net' started by Andrew Robinson, Jul 17, 2006.

  1. Is there any way to specify a generic class as the TypeName for an ODS? Or
    do I just need to do the same thing and wrap the generic class and in
    inherited class?

    Example:

    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
    DataObjectTypeName="Department" TypeName="MyDataHandler<Department>"
    SelectMethod="Select" />

    where:

    [DataObject(true)]
    public class MyDataHandler<D>
    {
    public MyDataHandler()
    {
    }

    [DataObjectMethod(DataObjectMethodType.Select, true)]
    public List<D> Select()
    {
    List<D> list = new List<D>();

    // build the list

    return list;
    }
    }
    Andrew Robinson, Jul 17, 2006
    #1
    1. Advertising

  2. Hi Andrew,

    Thank you for your post.

    Based on my understanding, your question is how to use a generics class in
    ObjectDataSource. If I've misunderstood anything, please feel free to post
    here.

    Internally, the ObjectDataSource is calling Type.GetType(string) to get the
    type, so we need to follow the guideline documented in Type.GetType on how
    to get type using generics. You can refer to MSDN Library on Type.GetType:

    http://msdn2.microsoft.com/en-us/library/w3f99sx1.aspx

    From the document, you will learn that you need to use backtick (`) to
    denotes the type name which is using generics.

    Also, here we must specify the assembly name in the type name string.

    So, for your question, the answer is to use type name like follows:

    TypeName="TestObjectDataSourceAssembly.MyDataHandler`1[System.String],TestOb
    jectDataSourceAssembly"

    If the class is placed in your App_Code subfolder of the web application,
    please use "__code" as the specicial assembly name, i.e.:

    TypeName="TestObjectDataSourceAssembly.MyDataHandler`1[System.String],__code
    "

    Hope this helps. Please feel free to post here if anything is unclear.

    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Walter Wang [MSFT], Jul 18, 2006
    #2
    1. Advertising

  3. Walter,

    Thanks, I think this is exactly what I was looking for. Will give it a try.

    -A

    "Walter Wang [MSFT]" <> wrote in message
    news:$...
    > Hi Andrew,
    >
    > Thank you for your post.
    >
    > Based on my understanding, your question is how to use a generics class in
    > ObjectDataSource. If I've misunderstood anything, please feel free to post
    > here.
    >
    > Internally, the ObjectDataSource is calling Type.GetType(string) to get
    > the
    > type, so we need to follow the guideline documented in Type.GetType on how
    > to get type using generics. You can refer to MSDN Library on Type.GetType:
    >
    > http://msdn2.microsoft.com/en-us/library/w3f99sx1.aspx
    >
    > From the document, you will learn that you need to use backtick (`) to
    > denotes the type name which is using generics.
    >
    > Also, here we must specify the assembly name in the type name string.
    >
    > So, for your question, the answer is to use type name like follows:
    >
    > TypeName="TestObjectDataSourceAssembly.MyDataHandler`1[System.String],TestOb
    > jectDataSourceAssembly"
    >
    > If the class is placed in your App_Code subfolder of the web application,
    > please use "__code" as the specicial assembly name, i.e.:
    >
    > TypeName="TestObjectDataSourceAssembly.MyDataHandler`1[System.String],__code
    > "
    >
    > Hope this helps. Please feel free to post here if anything is unclear.
    >
    > Regards,
    > Walter Wang (, remove 'online.')
    > Microsoft Online Community Support
    >
    > ==================================================
    > When responding to posts, please "Reply to Group" via your newsreader so
    > that others may learn and benefit from your issue.
    > ==================================================
    >
    > This posting is provided "AS IS" with no warranties, and confers no
    > rights.
    >
    Andrew Robinson, Jul 18, 2006
    #3
  4. Hi Andrew,

    Thank you for your quick reply. Please let me know whether or not this
    suggestion works. I'll be glad to follow up if you have anything unclear.

    Have a nice day!

    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Walter Wang [MSFT], Jul 18, 2006
    #4
  5. Walter,

    Works as advertised. I would like to know if there is any history behind the
    notation. I can see the [] brackets since we are dealing with the CLR and
    not C# but where does the `1 come from? Couldn't GetType simply count the
    number of generic types within the [] brackets?

    Again, it all makes complete sense but would love to understand a bit more.

    Thanks,


    "Walter Wang [MSFT]" <> wrote in message
    news:...
    > Hi Andrew,
    >
    > Thank you for your quick reply. Please let me know whether or not this
    > suggestion works. I'll be glad to follow up if you have anything unclear.
    >
    > Have a nice day!
    >
    > Regards,
    > Walter Wang (, remove 'online.')
    > Microsoft Online Community Support
    >
    > ==================================================
    > When responding to posts, please "Reply to Group" via your newsreader so
    > that others may learn and benefit from your issue.
    > ==================================================
    >
    > This posting is provided "AS IS" with no warranties, and confers no
    > rights.
    >
    Andrew Robinson, Jul 18, 2006
    #5
  6. Hi Andrew,

    Thank you for your quick reply.

    The number after the backtick is the count of generic type parameters. The
    reason for this name mangling is to support overloading of type based on
    generic type arity (Arity: the number of type parameters on a generic type
    or method). For example, you could define another type MyDataHandler<T,U>,
    then in metadata we can have two different name: MyDataHandler`1 and
    MyDataHandler`2.

    This notation is actually burned in at the metadata level as the string
    type name. GetType() takes an unmangled string name and simply brings it to
    life by string comparing over the metadata table. It's unfortunate that we
    couldn't use angle brackets to specify arity, ala C# syntax, but the C++
    folk have already reserved them in the type name grammar.

    Hopefully this explains both the backtick+count and bracket notations in
    metadata, please feel free to post here if anything is unclear.



    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Walter Wang [MSFT], Jul 19, 2006
    #6
  7. Hi Andrew,

    Does my last reply answer your question? If you have any other questions or
    concerns, please do not hesitate to contact us.

    Have a nice day!

    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Walter Wang [MSFT], Jul 24, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dotnet Gruven
    Replies:
    1
    Views:
    5,179
    Steven Cheng[MSFT]
    Sep 14, 2005
  2. Replies:
    2
    Views:
    4,071
  3. Replies:
    2
    Views:
    435
  4. David Thielen

    ObjectDataSource method as another ObjectDataSource

    David Thielen, Mar 21, 2006, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    242
    Steven Cheng[MSFT]
    Mar 23, 2006
  5. André Silva

    ObjectDataSource with Generic Class

    André Silva, Oct 10, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    121
    André Silva
    Oct 10, 2006
Loading...

Share This Page