托管的C ++/CLI函数CreateInstanceFromAndUnwrap类型转换问题. [英] Managed C++ /CLI function CreateInstanceFromAndUnwrap typecast problem.

查看:135
本文介绍了托管的C ++/CLI函数CreateInstanceFromAndUnwrap类型转换问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 在程序中使用函数CreateInstanceFromAndUnwrap(....)时遇到问题.

问题:
我确实有某些c#dll,我必须在我的c ++/cli代码中加载.之后,我想从该程序集中调用某些函数.例如,我的C:\驱动器中确实有一个dll(C#),该dll包含一个类称为ABC的类.
我想对ABC对象调用某些属性,方法.

Hi I am facing a problem while using function CreateInstanceFromAndUnwrap(....) in my program.

Problem:
I do have certain c# dll which i have to load in my c++/cli code.and after that i want to call certain function from that assembly. For example, I do have a dll(C#) in my C:\ drive.This dll contain a class say ABC.
I want to call certain properties,methods on the object of ABC.

//my sample code part 1 for AppDomain::CreateInstanceFromAndUnwrap
//New app domain is created
AppDomainSetup^ appDomainInfo = gcnew AppDomainSetup();
appDomainInfo->ApplicationBase = strDefaultPath;//Path of dll(c#)
AppDomain^ newAppDom = AppDomain::CreateDomain(strDomainName,nullptr,appDomainInfo);
Object^ obj = newAppDom->CreateInstanceFromAndUnwrap(strPath,strClassName );//Line 4
Type^ tAsmbly = obj->GetType();
String^ sFullName = tAsmbly->AssemblyQualifiedName;
PropertyInfo^ pInfo  = tAsmbly->GetProperty("AddInType");//property not found
String^ sTypeName = (String^)pInfo->GetValue(obj,nullptr);

//my sample code part 2 for Assembly::LoadFrom
Assembly^ asm = Assembly::LoadFrom(strPath);
Object^ obj = asm->CreateInstance(strClassName); 
Type^ tAsmbly = obj->GetType();
String^ sFullName = tAsmbly->AssemblyQualifiedName;
PropertyInfo^ pInfo = tAsmbly->GetProperty("AddInType");//Property Found
String^ sTypeName = (String^)pInfo->GetValue(obj,nullptr);

//In part 1 i cant typecast the object with the class name because it is in only in string format.


注意:
在第1部分中, sFullName是System.MarshalByRefObject,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089 .
在第2部分中, sFullName是实际的全名,与在第2部分中不同

问题:我只想知道我能否在两个部分中获得相同的全名.我的意思是在第1部分中,我想获得实际的装配全名.我无法将其强制转换为所需的类,所以我想通过反射我需要一些东西.

感谢


Note:
In part 1 sFullName is System.MarshalByRefObject, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.
In part 2 sFullName is Actual full name not like above part 2

Question: I just want to know can I get the Same full name in both part. I mean in part 1 I want to get the actual assembly full name. I cant type cast it to the desired class so I need some thing by reflection I guess.

Thanks

推荐答案

仅需注意:这可能是问题的根源:第一个参数assemblyName不是路径名.如果从当前运行的程序集中返回以下字符串,则可以看到期望的结果

Just one note: this can be a source of the problem: first parameter assemblyName is not the path name. You can see what''s expected if you return the following string from the currently running assembly

//for example:
string name = System.Reflection.Assembly.GetCallingAssembly().Name;



它应该是带有版本等的名称,签名令牌是强名称等.

另外,请不要忘记:类型名称(下一个参数)应完全限定.

—SA



It should be a name with version, etc., a signature token is a strong name, etc.

Also, don''t forget: the name of the type (next parameter) should be fully qualified.

—SA


这篇关于托管的C ++/CLI函数CreateInstanceFromAndUnwrap类型转换问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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