[英]Pass C# object to COM by value
I'm trying to use a COM interface from C# that exposes the following interface as generated by tlbimp: 我正在尝试使用来自C#的COM接口,它暴露了由tlbimp生成的以下接口:
[Guid("7DDCEDF4-3B78-460E-BB34-C7496FD3CD56")]
[TypeLibType(TypeLibTypeFlags.FDual | TypeLibTypeFlags.FNonExtensible | TypeLibTypeFlags.FDispatchable)]
public interface IFred
{
[DispId(1)]
IBarney Pall { get; set; }
}
[Guid("E390230E-EE9C-4819-BC19-08DAB808AEA9")]
[TypeLibType(TypeLibTypeFlags.FDual | TypeLibTypeFlags.FNonExtensible | TypeLibTypeFlags.FDispatchable)]
public interface IBarney
{
[DispId(1)]
double Wilma { get; set; }
}
The generated wrapper assembly does not contain an implementation for the IBarney
interface. 生成的包装程序集不包含
IBarney
接口的实现。 I've created a C# structure that implements the IBarney
interface like this: 我已经创建了一个C#结构来实现
IBarney
接口,如下所示:
[Guid("2C61BA37-7047-43DB-84B1-83B4268CF77B")]
[ComVisible(true)]
public struct Barney : IBarney
{
public double Wilma { get; set; }
}
Which "works", the question now is, will a Barney
instance be marshalled by value or by reference? 现在的问题是“有效”,
Barney
实例是否会按价值或参考进行整理? This is important due to the network overhead involved. 由于涉及网络开销,这很重要。 Ideally executing something like:
理想情况下执行以下操作:
fredInstance.Pall = new Barney { Wilma = 0.37 };
will result in a single network round trip. 将导致单个网络往返。 How can I verify this, or how can I tell COM interop my
Barney
structure should always be marshalled by value? 我如何验证这一点,或者如何告诉COM互操作我的
Barney
结构应该始终按值进行编组?
Given the comment from Hans Passant. 鉴于Hans Passant的评论。 What would be the 'proper' way to design this?
设计这个的“正确”方法是什么? What would be the IDL needed to allow for a simple structure to be usable as a value for a 'property' of a COM interface?
允许简单结构作为COM接口的“属性”值使用的IDL是什么? Looking at the IDL from which the interfaces are generated that I'm currently using adding a
coclass
declaration with a default IBarney
interface would be enough, right? 查看生成接口的IDL,我当前正在使用添加带有默认
IBarney
接口的coclass
声明就足够了,对吧?
A couple comments... Don't know if this will be an answer or not... 几个评论......不知道这是不是答案......
1) I would think that you should make your interfaces [ComVisible(true)] 1)我认为你应该制作你的接口[ComVisible(true)]
2) Why do you define Barney as a struct? 2)为什么要将Barney定义为结构? It should be a class.
它应该是一个班级。 Unlike C++ where the only difference between a class and a struct is the default of private vs public members, C# structs and classes are fundamentally different.
与C ++不同,类和结构之间的唯一区别是私有vs公共成员的默认值,C#结构和类从根本上是不同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.