公开C#匿名类对象,COM(JavaScript的) [英] Exposing c# anonymous class objects to COM (JavaScript)

查看:133
本文介绍了公开C#匿名类对象,COM(JavaScript的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有类/ API在.NET 4.5 +允许C#匿名类的一个实例公开为迟绑定对象到COM?例如。我想做到这一点:

Is there a class/API in .NET 4.5+ allowing to expose an instance of C# anonymous class as as late-bound object to COM? E.g. I want do this:

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

和使用它在JavaScript:

and use it in JavaScript:

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

这不应该是一个问题,创建一个辅助类来包装匿名对象,并通过的 IReflect ,但也许,这样的事情已经存在?只是不想推倒重来。

It should not be a problem to create a helper class to wrap the anonymous object and expose its properties via IReflect, but perhaps, something like this already exists? Just don't want to reinvent the wheel.

推荐答案

我煮了一个辅助类来做到这一点,在此基础上的优秀的博客帖子

I've cooked up a helper class to make it happen, based on this excellent blog post.

用法:

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

code:

Code:

[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#匿名类对象,COM(JavaScript的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆