本文介绍了为什么我在代码隐藏类中使用泛型方法会导致动态编译异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b $对我正在开发的传统ASP.Net Web窗体应用程序进行一些更改后,每当我尝试访问应用程序的页面时,都会收到以下异常。 b

故障排除,我最终将问题缩小到了我添加到应用程序中的所有页面从其派生的基页类的方法。称它为 MyBasePageClass 。

  protected T CreatePlaceholder< T>(T item )其中T:MyDataModelClass,new()
{
返回新的T {TemporaryIdentifier = item.TemporaryIdentifier};
}

当我注释掉这个方法时,异常就消失了,会期待。如果我在web.config文件中添加程序集引用,这个异常也会消失。奇怪的是,当我将引用复制到本地= true时,这也使异常消失。



这不是第一次将MyDataModelClass合并到应用程序中。它几乎在每个代码后面的文件中都用到了很多地方。但由于某种原因,使用它作为类型约束不止.Net可以处理。



我只能想象它与泛型的本质有关,但我不知道这可能是什么。我不能成为第一个尝试过这种方法的人,但我无法在SO或其他任何地方找到任何有关它的信息。它可能与IIS的配置有关吗?



更新:



MyDataModelAssembly确实安装在GAC中。这是gacutil / l的输出:

lockquote
全局程序集缓存包含以下程序集:
MyDataModelAssembly,Version = 40.0。 0.30,Culture = neutral,PublicKeyToken = 7ca3fb5049101832,processorArchitecture = MSIL


解决方案

2:
这看起来像解决您的问题:检查什么是在GAC中。在我的机器上,gacutil.exe位于'C:\程序文件(x86)\ Microsoft SDKs \Windows\v10.0A\bin\NETFX 4.6.1 Tools'中,但它可能位于其他位置你的机器。一旦找到它,从命令行运行:

lockquote
gacutil / l MyDataModelAssembly

gacutil输出的版本和公钥令牌必须与您在错误消息中看到的内容匹配。






定义 MyDataModelClass 的程序集DLL必须复制到IIS运行应用程序的位置。



复制本地设置告诉Visual Studio将DLL复制到编译输出路径(这可能是IIS运行应用程序的位置)。对于任何不在。

After making some changes to a legacy ASP.Net Web Forms app I'm working on, I started receiving the following exception whenever I tried to visit a page of the application.

After hours of troubleshooting, I finally narrowed down the problem to a method I had added to a base page class from which all pages in the application derive. Call it MyBasePageClass.

protected T CreatePlaceholder<T>(T item) where T : MyDataModelClass, new()
{
    return new T { TemporaryIdentifier = item.TemporaryIdentifier };
}

When I comment out that method, the exception goes away and everything works as I would expect. If I add an assembly reference in the web.config file, this exception also goes away. Oddly, when I change the reference to Copy Local = true, this also makes the exception go away.

This is not the first time that MyDataModelClass has been incorporated into the application. It is used in scores of places in almost every code behind file. But for some reason, using it as a type constraint is more than .Net can handle.

I can only imagine that It has something to do with the nature of generics, but I have no idea what that might be. I cannot be the first to have tried this, but I can't find any information about it on SO or anywhere else. Could it have something to do with how IIS is configured?

Update:

MyDataModelAssembly is indeed installed in the GAC. Here is the output of gacutil /l:

解决方案

Edit 2:This looks like the solution to your problem: Strange Error - CS0012: The type x is defined in an assembly that is not referenced

But no real explanation why.


Edit:

Since your assembly is in the GAC, it should be finding it and not require Copy Local.

Make sure the version in the GAC is the version it's looking for. You can use Gacutil to check what's in the GAC. On my machine, gacutil.exe is in 'C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools', but it might be somewhere else on your machine. Once you find it, run this from the command line:

The version and public key token output by gacutil must match what you see in the error message.


Your assembly DLL that defines MyDataModelClass has to be copied to the location that IIS is running the application from.

The Copy Local setting tells Visual Studio to copy the DLL to the compile output path (which is likely where IIS is running the application from). You will need Copy Local set to true for any assembly that is not in the GAC.

这篇关于为什么我在代码隐藏类中使用泛型方法会导致动态编译异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 08:34