English 中文(简体)
运行时包装器 (yùn xíng shí bāo zhuāng qì)
原标题:
  • 时间:2009-03-26 09:26:34
  •  标签:

i got one COM component and want to access from C#.I created a interface in my C# program and want to access that component with its CoClass.When i created a object it gives me __ComObject which is a RuntimeCallabelWrapper.

I want to know when it returns me a object and it create a MethodTable for me, then when i cast this interface to another interface to call method of other interface i adds method of second interface or create a new MethodTable for that? Below is the code:

[Guid("169D0491-E149-4BB3-932E-A42C4F8D3478")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
interface IInventory
{
    void Buy(int quantity, float rate);
    void Sell(int quantity, float rate);
    int GetStock();
}

[Guid("8B08A7ED-8D46-41B2-B8D9-35F4CF37F75B")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IAccount
{
    void Credit(double amount);
    void Debit(double amount);
    double GetBalance();
}


[Guid("F8E3E05A-CAFA-4CBD-BC42-DF9328729B01")]
[ComImport]
class Trader{}

public static void Main(string[] args)
{
    IInventory inv = (IInventory) new Trader(); // this gives me RCW
    inv.Buy(qty, 150);

    IAccount acc = (IAccount) inv;   // ?
    Console.WriteLine("Current balance: {0}", acc.GetBalance());
}
问题回答

库存 (IInventory) 和账户 (IAccount) 是完全独立的 (除了它们都继承自IUnknown),你不能将一个转换成另一个。除非 Trader 类实现了它们俩,否则不能转换。





相关问题
热门标签