Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ObjectDataSource with Generic Class

72 views
Skip to first unread message

Andrew Robinson

unread,
Jul 17, 2006, 6:52:30 PM7/17/06
to
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;
}
}


Walter Wang [MSFT]

unread,
Jul 17, 2006, 11:41:30 PM7/17/06
to
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 (waw...@online.microsoft.com, 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

unread,
Jul 18, 2006, 12:56:25 AM7/18/06
to
Walter,

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

-A

"Walter Wang [MSFT]" <waw...@online.microsoft.com> wrote in message
news:$GZxWwhq...@TK2MSFTNGXA01.phx.gbl...

Walter Wang [MSFT]

unread,
Jul 18, 2006, 4:17:55 AM7/18/06
to
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!

Andrew Robinson

unread,
Jul 18, 2006, 11:40:28 AM7/18/06
to
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]" <waw...@online.microsoft.com> wrote in message

news:1pSEyKkq...@TK2MSFTNGXA01.phx.gbl...

Walter Wang [MSFT]

unread,
Jul 18, 2006, 10:41:50 PM7/18/06
to
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.

Walter Wang [MSFT]

unread,
Jul 24, 2006, 2:44:53 AM7/24/06
to
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!

0 new messages