Exposing c# anonymous class objects to COM (JavaScript)

前端 未结 1 1955
时光取名叫无心
时光取名叫无心 2021-01-20 20:31

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.Docume         


        
相关标签:
1条回答
  • 2021-01-20 20:49

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

    Usage:

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

    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
    }
    
    0 讨论(0)
提交回复
热议问题