C# 将泛型对象强制转换为com类的类型失败
我有一个COM类,它注册到ROT中,通过另一个应用程序,我从ROT中获取COM类的实例,并将其转换为其类型,给出以下错误 无法将“System.\u ComObject”类型的COM对象强制转换为接口类型“ROTViewer.IHandleVIParentInfo”。此操作失败,因为对具有IID“{C4B3F18E-FDF1-3F0C-A6DB-F03B302CAE9F}”的接口的COM组件的QueryInterface调用失败,原因是以下错误:不支持此类接口(HRESULT的异常:0x80004002(E_NOINTERFACE))。 下面是我的COM类实现C# 将泛型对象强制转换为com类的类型失败,c#,casting,com,running-object-table,C#,Casting,Com,Running Object Table,我有一个COM类,它注册到ROT中,通过另一个应用程序,我从ROT中获取COM类的实例,并将其转换为其类型,给出以下错误 无法将“System.\u ComObject”类型的COM对象强制转换为接口类型“ROTViewer.IHandleVIParentInfo”。此操作失败,因为对具有IID“{C4B3F18E-FDF1-3F0C-A6DB-F03B302CAE9F}”的接口的COM组件的QueryInterface调用失败,原因是以下错误:不支持此类接口(HRESULT的异常:0x8000
public interface IHandleVIParentInfo
{
Dictionary<int, string> ContainerParentredVi {get; set;}
int GetListCount();
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[ComSourceInterfaces(typeof(IHandleVIParentInfo))]
[ProgIdAttribute("Qcs.VIParentedInfoHandler")]
[GuidAttribute(HandleVIParentedInfo.Guid)]
[DisplayName("Qcs VI Parented Info Handler")]
public class HandleVIParentedInfo : IDisposable, IHandleVIParentInfo
{
public const string Guid = "CB6BFC97-F8E3-4fce-B68B-4D01485811A1";
public Dictionary<int, string> ContainerParentredVi
{
get
{
return _containerParentredVi;
}
set
{
_containerParentredVi = value;
}
}
private Dictionary<int, string> _containerParentredVi = new Dictionary<int, string>();
public HandleVIParentedInfo()
{
//private Dictionary<int, string> _containerParentredVi = new Dictionary<int, string>();
}
#region IDisposable Members
void IDisposable.Dispose()
{
throw new NotImplementedException();
}
#endregion
#region IHandleVIParentInfo Members
int IHandleVIParentInfo.GetListCount()
{
return ContainerParentredVi.Count;
}
#endregion
公共接口IHandleVIParentInfo
{
字典容器parentredvi{get;set;}
int GetListCount();
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[ComSourceInterfaces(typeof(IHandleVIParentInfo))]
[项目属性(“Qcs.VIParentedInfoHandler”)]
[GuidAttribute(HandleVIParentedInfo.Guid)]
[显示名称(“Qcs VI父代信息处理程序”)]
公共类handleviparentInfo:IDisposable,IHandleVIParentInfo
{
公用常量字符串Guid=“CB6BFC97-F8E3-4fce-B68B-4D01485811A1”;
公共字典int,字符串ContainerParentredVi
{
收到
{
返回(u container parentredvi);;
}
设置
{
_containerParentredVi=值;
}
}
private Dictionaryint,string _containerParentredVi=new Dictionaryint,string();
公共管理信息
{
//private Dictionary YNT,string U containerParentTredvi=新字典YNT,string();
}
#区域IDisposable成员
void IDisposable.Dispose()无效
{
抛出新的NotImplementedException();
}
#端区
#区域IHandleVIParentInfo成员
int IHandleVIParentInfo.GetListCount()
{
返回ContainerParentredVi.Count;
}
#端区
下面是我将对象类型转换为com类类型的方法
public static void GetIDEInstances()
{
instances = new List<object>();
Hashtable runningStationInstances = new Hashtable();
Hashtable runningObjects = GetROTContent();
object expectedObj;
//runningObjects["!{5EB461D8-71CD-4E78-A360-4CE788A93063}"].GetHashCode();
IDictionaryEnumerator rotEnumerator = runningObjects.GetEnumerator();
while (rotEnumerator.MoveNext())
{
string candidateName = (string)rotEnumerator.Key;
if (string.Compare(candidateName,"!{CB6BFC97-F8E3-4fce-B68B-4D01485811A1}",true)==0)
{
viParenting = (IHandleVIParentInfo)rotEnumerator.Value;
}
}
}
publicstaticvoid GetIDEInstances()
{
实例=新列表();
Hashtable runningStationInstances=新Hashtable();
Hashtable runningObjects=GetROTContent();
预期对象bj;
//runningObjects[“!{5EB461D8-71CD-4E78-A360-4CE788A93063}]”。GetHashCode();
IDictionaryEnumerator rotEnumerator=runningObjects.GetEnumerator();
while(rotEnumerator.MoveNext())
{
字符串candidateName=(字符串)rotEnumerator.Key;
if(string.Compare(candidateName,“!{CB6BFC97-F8E3-4fce-B68B-4D01485811A1}”,true)==0)
{
viParenting=(IHandleVIParentInfo)rotEnumerator.Value;
}
}
}
在“viParenting=(IHandleVIParentInfo)rotEnumerator.Value;”行中获取错误。您在接口声明中忘记了[ComVisible(true)]。必填项。我使接口COM可见,这对我没有帮助:(