IsComImport() public static méthode

public static IsComImport ( Type t ) : bool
t System.Type
Résultat bool
Exemple #1
0
        public DynamicComObjectWrapper(COMRegistry registry, Type instanceType, object entry)
        {
            _registry = registry;

            if (instanceType == null)
            {
                throw new ArgumentNullException("instanceType");
            }

            if (entry == null)
            {
                throw new ArgumentNullException("entry");
            }

            if (!COMUtilities.IsComImport(instanceType))
            {
                throw new ArgumentException("Interface type must be an imported COM type");
            }

            if (!Marshal.IsComObject(entry))
            {
                throw new ArgumentException("Target must be a COM object");
            }

            _methods    = instanceType.GetMethods().Where(m => !m.IsSpecialName).ToDictionary(m => m.Name);
            _properties = instanceType.GetProperties().ToDictionary(m => m.Name);

            _target       = entry;
            _instanceType = instanceType;
        }
        public static BaseComWrapper Wrap(object obj, Type intf_type)
        {
            if (intf_type == null)
            {
                throw new ArgumentNullException("No interface type available", nameof(intf_type));
            }

            if (!COMUtilities.IsComImport(intf_type) || !intf_type.IsInterface || !intf_type.IsPublic)
            {
                throw new ArgumentException("Wrapper type must be a public COM interface");
            }

            if (intf_type.Assembly.ReflectionOnly)
            {
                throw new ArgumentException("Interface type cant be reflection only", nameof(intf_type));
            }

            if (!Marshal.IsComObject(obj))
            {
                throw new ArgumentException("Object must be a COM object", nameof(obj));
            }

            if (!_types.ContainsKey(intf_type.GUID))
            {
                Type        base_type = typeof(BaseComWrapper <>).MakeGenericType(intf_type);
                TypeBuilder tb        = _module.DefineType(
                    $"{intf_type.Name}Wrapper",
                    TypeAttributes.Public | TypeAttributes.Sealed, base_type);
                tb.AddInterfaceImplementation(intf_type);
                var con   = tb.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(object) });
                var conil = con.GetILGenerator();
                conil.Emit(OpCodes.Ldarg_0);
                conil.Emit(OpCodes.Ldarg_1);
                conil.Emit(OpCodes.Call,
                           base_type.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(object) }, null));
                conil.Emit(OpCodes.Ret);
                foreach (var mi in intf_type.GetMethods().Where(m => (m.Attributes & MethodAttributes.SpecialName) == 0))
                {
                    GenerateForwardingMethod(tb, mi, 0, base_type);
                }

                foreach (var pi in intf_type.GetProperties())
                {
                    var pb = tb.DefineProperty(pi.Name, PropertyAttributes.None, pi.PropertyType, pi.GetIndexParameters().Select(p => p.ParameterType).ToArray());
                    if (pi.CanRead)
                    {
                        var get_method = GenerateForwardingMethod(tb, pi.GetMethod, MethodAttributes.HideBySig | MethodAttributes.SpecialName, base_type);
                        pb.SetGetMethod(get_method);
                    }
                    if (pi.CanWrite)
                    {
                        var set_method = GenerateForwardingMethod(tb, pi.SetMethod, MethodAttributes.HideBySig | MethodAttributes.SpecialName, base_type);
                        pb.SetSetMethod(set_method);
                    }
                }

                _types[intf_type.GUID] = tb.CreateType();
            }
            return((BaseComWrapper)Activator.CreateInstance(_types[intf_type.GUID], obj));
        }
Exemple #3
0
        public static object Wrap(COMRegistry registry, object o, Type objType)
        {
            if ((o != null) && !(o is DynamicComObjectWrapper) && COMUtilities.IsComImport(objType))
            {
                return(new DynamicComObjectWrapper(registry, objType, o));
            }

            return(o);
        }
        private void DoRunScript(object sender, PythonScriptEditor.RunScriptEventArgs e)
        {
            try
            {
                ScriptEngine engine = Python.CreateEngine();

                engine.Runtime.LoadAssembly(Assembly.GetExecutingAssembly());
                engine.Runtime.IO.SetOutput(new MemoryStream(), new ConsoleTextWriter(this, false));
                engine.Runtime.IO.SetErrorOutput(new MemoryStream(), new ConsoleTextWriter(this, true));

                ICollection <string> paths = engine.GetSearchPaths();

                paths.Add(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PythonLib"));

                engine.SetSearchPaths(paths);

                ScriptErrorListener listener = new ScriptErrorListener();
                ScriptSource        source   = engine.CreateScriptSourceFromString(e.ScriptText);

                CompiledCode code = source.Compile(listener);

                if (listener.Errors.Count == 0)
                {
                    // Just create the global scope, don't execute it yet
                    ScriptScope scope = engine.CreateScope();

                    scope.SetVariable("obj", COMUtilities.IsComImport(m_dispType)
                        ? new DynamicComObjectWrapper(m_registry, m_dispType, m_pObject) : m_pObject);
                    scope.SetVariable("disp", m_pObject);

                    dynamic host = new ExpandoObject();

                    host.openobj = new Action <DynamicComObjectWrapper>(o => { OpenObjectViewer(o); });

                    scope.SetVariable("host", host);

                    code.Execute(scope);
                }
            }
            catch (Exception ex)
            {
                TargetInvocationException tex = ex as TargetInvocationException;

                if (tex != null)
                {
                    ex = tex.InnerException;
                }

                AddText(ex.Message + Environment.NewLine);
            }
        }