使用impromptu-interface时如何访问 Duck Typed 代理对象。考虑我的代码,该代码说明了我在InvalidCastException
尝试将 Duck Typed Object 转换为代理对象时得到的示例:
using System;
using ImpromptuInterface;
namespace ConsoleApplication1
{
public class Duck
{
public string Says { get; set; }
public int GetNumberOfQuacksPerMinute()
{
return 42;
}
}
public interface IPondBird
{
string Says { get; set; }
}
class Program
{
static void Main(string[] args)
{
// Duck says Quack! Quack!! Quack!!!
var thing = new Duck { Says = "Quack! Quack!! Quack!!!" };
IPondBird myInterface = Impromptu.ActLike(thing);
// ...
// Later on, I want to get access to a proxied object, but I
// get a InvalidCastException here
Duck proxiedObject = (Duck) myInterface;
Console.WriteLine("Duck # quacks per minute: "
+ proxiedObject.GetNumberOfQuacksPerMinute());
}
}
}
例外情况如下:
ConsoleApplication1.exe 中发生了“System.InvalidCastException”类型的未处理异常
附加信息:无法将类型为“ActLike_IPondBird_c7dd53902ec74f01a3844d4789244ea3”的对象转换为类型“ConsoleApplication1.Duck”。