Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法将类型对象隐式转换为System.Reflection.Assembly_C#_.net_Reflection - Fatal编程技术网

C# 无法将类型对象隐式转换为System.Reflection.Assembly

C# 无法将类型对象隐式转换为System.Reflection.Assembly,c#,.net,reflection,C#,.net,Reflection,请帮助我修复此错误: 无法将类型“object”隐式转换为“System.Reflection.Assembly”。存在显式转换(是否缺少强制转换?) 谢谢你 代码 var assemblyName = AssemblyName.GetAssemblyName(assemblyPath); var ads = new AppDomainSetup { ApplicationBase = AppDomain.CurrentDoma

请帮助我修复此错误:

无法将类型“object”隐式转换为“System.Reflection.Assembly”。存在显式转换(是否缺少强制转换?)

谢谢你

代码

var assemblyName = AssemblyName.GetAssemblyName(assemblyPath);

var ads = new AppDomainSetup
               {
                  ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase, 
                  DisallowCodeDownload = true 
               };

AppDomain newDomainName = AppDomain.CreateDomain("newDomainName", null, ads);

Assembly testLibrary = newDomainName.CreateInstanceAndUnwrap(assemblyName.Name,
                      typeof(System.Windows.Controls.UserControl).Name);  // Here the error occures

尝试使用
FullName
属性:

typeof(System.Windows.Controls.UserControl).FullName
编辑:

还可以尝试assemblyName.FullName


根据需要,两者都应该是全名。

尝试使用
FullName
属性:

typeof(System.Windows.Controls.UserControl).FullName
编辑:

还可以尝试assemblyName.FullName

根据这两种方法,都应该是全名。

返回所创建类型的实例(
上面代码中的System.Windows.Controls.UserControl
),而不是
程序集。代码应为:

UserControl userControl = (UserControl) newDomainName.CreateInstanceAndUnwrap(
    assemblyName.Name, typeof(System.Windows.Controls.UserControl).Name);
返回所创建类型的实例(
System.Windows.Controls.UserControl
,在上面的代码中),而不是
程序集
。代码应为:

UserControl userControl = (UserControl) newDomainName.CreateInstanceAndUnwrap(
    assemblyName.Name, typeof(System.Windows.Controls.UserControl).Name);
它返回一个对象,您需要一个程序集,请重新检查


它返回一个对象,您需要一个程序集,请重新检查。

尝试更正与typeof(System.Windows.Controls.UserControl).Name)匹配的括号。您缺少一个开头(例如:typeof((System.Windows.Controls.UserControl).Name)@Freeman否,括号是正确的。第一个右括号属于
typeof
,第二个是
CreateInstanceAndUnwrap
的右括号。我不明白您想做什么。您似乎正在新域中创建
UserControl
,然后将其存储在
Assembly
类型的变量中。这对我来说没有意义。编辑:除此之外,您没有传递包含
UserControl
类型的程序集的程序集名称,因此它无论如何都不会工作。
CreateInstanceAndUnwrap
的返回值不是
程序集
。那么,为什么您要将
testLibrary
声明为一个呢?请尝试更正与typeof(System.Windows.Controls.UserControl.Name)匹配的括号。您缺少一个开头,例如:typeof((System.Windows.Controls.UserControl.Name)@Freeman不,括号是正确的。第一个右括号属于
typeof
,第二个是
CreateInstanceAndUnwrap
的右括号。我不明白您想做什么。您似乎正在新域中创建
UserControl
,然后将其存储在
Assembly
类型的变量中。这对我来说没有意义。编辑:除此之外,您没有传递包含
UserControl
类型的程序集的程序集名称,因此它无论如何都不会工作。
CreateInstanceAndUnwrap
的返回值不是
程序集
。那么,为什么您将
testLibrary
声明为一个呢?“您需要一个类型”——嗯
testLibrary
被声明为具有类型
Assembly
“并且您需要一个类型”--嗯<代码>测试库
声明为具有类型
程序集