使用泛型类与ObjectDataSource控件 [英] Using generic classes with ObjectDataSource

查看:132
本文介绍了使用泛型类与ObjectDataSource控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通用的存储库&LT; T&GT;类我想与一个ObjectDataSource使用。存储库&LT; T&GT;住在所谓的数据访问一个单独的项目。据<一个href=\"http://groups.google.com/group/microsoft.public.dotnet.framework.aspnet/browse_thread/thread/767f1a821d9b23da/b1e045958ae427a5?lnk=st#b1e045958ae427a5\">this

:从MS新闻组(下复制相关部分)发布

  

在内部,ObjectDataSource控件在调用Type.GetType(字符串),以获得
  类型,所以我们需要按照Type.GetType记录如何指引
  使用泛型得到的类型。你可以参考MSDN库上Type.GetType:


  
  

<一个href=\"http://msdn2.microsoft.com/en-us/library/w3f99sx1.aspx\">http://msdn2.microsoft.com/en-us/library/w3f99sx1.aspx


  
  

从文档,你会知道你需要使用反引号(`)来
  表示这是使用泛型类型名称。


  
  

另外,在这里,我们必须在类型名称字符串指定程序集的名称。


  
  

因此​​,对于您的问题,答案是使用类型名像如下:


  
  

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


好吧,是有道理的。当我尝试它,但是,页面抛出一​​个异常:

 &LT; ASP:ObjectDataSource控件ID =MyDataSource的TypeName =MyProject.Repository`1 [MyProject.MessageCategory],数据访问/&GT;


  

[出现InvalidOperationException:在ObjectDataSource控件MyDataSource的TypeName属性指定的类型找不到]


好奇的是,当我正在浏览的页面这只发生。当我从VS2008设计师打开配置数据源对话框,它正确地显示了我对我的通用库类的方法。传递类型名字符串Type.GetType(),同时也调试返回一个有效的类型。那么怎么办?


解决方案

做这样的事情。

 键入类型= ty​​peof运算(库&LT; MessageCategory);
字符串assemblyQualifiedName = type.AssemblyQualifiedName;

得到assemblyQualifiedName的值,并将其粘贴到类型名场。需要注意的是Type.GetType(字符串),传递的值必须是


  

类型的程序集限定名称来获得。见<一href=\"http://msdn.microsoft.com/en-us/library/system.type.assemblyqualifiedname.aspx\">AssemblyQualifiedName.如果类型是在当前执行的程序集或在mscorlib.dll,它足以提供其命名空间限定的类型名称。


因此​​,它可以通过传递在code该字符串的工作,因为那类是在当前执行的程序集(你在哪里调用它),其中为ObjectDataSource控件是没有的。

最有可能你正在寻找的类型是

  MyProject.Repository`1 [MyProject.MessageCategory,数据访问,版本= 1.0.0.0,文化=中立,公钥= NULL],数据访问,版本= 1.0.0.0,文化=中性,公钥= NULL

I have a generic Repository<T> class I want to use with an ObjectDataSource. Repository<T> lives in a separate project called DataAccess. According to this post from the MS newsgroups (relevant part copied below):

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],TestObjectDataSourceAssembly"

Okay, makes sense. When I try it, however, the page throws an exception:

<asp:ObjectDataSource ID="MyDataSource" TypeName="MyProject.Repository`1[MyProject.MessageCategory],DataAccess" />

[InvalidOperationException: The type specified in the TypeName property of ObjectDataSource 'MyDataSource' could not be found.]

The curious thing is that this only happens when I'm viewing the page. When I open the "Configure Data Source" dialog from the VS2008 designer, it properly shows me the methods on my generic Repository class. Passing the TypeName string to Type.GetType() while debugging also returns a valid type. So what gives?

解决方案

Do something like this.

Type type = typeof(Repository<MessageCategory);
string assemblyQualifiedName = type.AssemblyQualifiedName;

get the value of assemblyQualifiedName and paste it into the TypeName field. Note that Type.GetType(string), the value passed in must be

The assembly-qualified name of the type to get. See AssemblyQualifiedName. If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.

So, it may work by passing in that string in your code, because that class is in the currently executing assembly (where you are calling it), where as the ObjectDataSource is not.

Most likely the type you are looking for is

MyProject.Repository`1[MyProject.MessageCategory, DataAccess, Version=1.0.0.0, Culture=neutral, PublicKey=null], DataAccess, Version=1.0.0.0, Culture=neutral, PublicKey=null

这篇关于使用泛型类与ObjectDataSource控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆