gpt4 book ai didi

c# - 将 c# 匿名类对象公开给 COM (JavaScript)

转载 作者:行者123 更新时间:2023-11-30 17:52:09 28 4
gpt4 key购买 nike

.NET 4.5+ 中是否有类/API 允许将 C# 匿名类的实例公开为 COM 的后期绑定(bind)对象?例如。我想这样做:

webBrowser.Document.InvokeScript("TestMethod", new object[] { 
new {
helloProperty = "Hello!",
byeProperty = "Bye!"
}
});

并在 JavaScript 中使用它:

function TestMethod(obj)
{
alert(obj.helloProperty + ", " + obj.byeProperty);
}

创建一个辅助类来包装匿名对象并通过IReflect公开其属性应该不是问题。 ,但也许,这样的事情已经存在了?只是不想重新发明轮子。

最佳答案

我已经根据 excellent blog post 编写了一个辅助类来实现它.

用法:

webBrowser.Document.InvokeScript("TestMethod", new object[] { 
new Reflector(new { helloProperty = "Hello!", byeProperty = "Bye!" }) });

代码:

[ComVisible(true), ClassInterface(ClassInterfaceType.None)]
public class Reflector :
System.Reflection.IReflect
{
object _target;

protected Reflector() { }

public Reflector(object target)
{
Debug.Assert(target != null);
_target = target;
}

public object Target
{
get { return _target; }
}

#region IReflect

public FieldInfo GetField(string name, BindingFlags bindingAttr)
{
return this._target.GetType().GetField(name, bindingAttr);
}

public FieldInfo[] GetFields(BindingFlags bindingAttr)
{
return this._target.GetType().GetFields(bindingAttr);
}

public MemberInfo[] GetMember(string name, BindingFlags bindingAttr)
{
return this._target.GetType().GetMember(name, bindingAttr);
}

public MemberInfo[] GetMembers(BindingFlags bindingAttr)
{
return this._target.GetType().GetMembers(bindingAttr);
}

public MethodInfo GetMethod(string name, BindingFlags bindingAttr)
{
return this._target.GetType().GetMethod(name, bindingAttr);
}

public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
{
return this._target.GetType().GetMethod(name, bindingAttr, binder, types, modifiers);
}

public MethodInfo[] GetMethods(BindingFlags bindingAttr)
{
return this._target.GetType().GetMethods(bindingAttr);
}

public PropertyInfo[] GetProperties(BindingFlags bindingAttr)
{
return _target.GetType().GetProperties(bindingAttr);
}

public PropertyInfo GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
{
return this._target.GetType().GetProperty(name, bindingAttr, binder, returnType, types, modifiers);
}

public PropertyInfo GetProperty(string name, BindingFlags bindingAttr)
{
return this._target.GetType().GetProperty(name, bindingAttr);
}

public object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, System.Globalization.CultureInfo culture, string[] namedParameters)
{
if (target == this)
{
if (name.CompareTo("[DISPID=0]") == 0)
{
if (invokeAttr.HasFlag(BindingFlags.InvokeMethod))
return this._target;
else if (invokeAttr.HasFlag(BindingFlags.GetProperty) && args.Length == 0)
return this._target.ToString();
}
else
{
return this._target.GetType().InvokeMember(name, invokeAttr, binder, _target, args, modifiers, culture, namedParameters);
}
}
throw new ArgumentException();
}

public Type UnderlyingSystemType
{
get { return this._target.GetType().UnderlyingSystemType; }
}

#endregion
}

关于c# - 将 c# 匿名类对象公开给 COM (JavaScript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18633071/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com