ObjectDataSource with Generic Class

A

Andrew Robinson

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

Walter Wang [MSFT]

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 ([email protected], 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.
 
A

Andrew Robinson

Walter,

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

-A
 
W

Walter Wang [MSFT]

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 ([email protected], 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.
 
A

Andrew Robinson

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,
 
W

Walter Wang [MSFT]

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 ([email protected], 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.
 
W

Walter Wang [MSFT]

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 ([email protected], 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top