Ejemplo n.º 1
0
        public static void Define(TypeBuilder typeB, Interface iface)
        {
            foreach (Method declMethod in iface.Methods)
            {
                //MethodBuilder method_builder = typeB.DefineMethod (declMethod.Name, MethodAttributes.Public | MethodAttributes.Virtual, declMethod.ReturnType, Mapper.GetTypes (ArgDirection.In, declMethod.GetParameters ()));

                List <Type> parms = new List <Type> ();

                if (declMethod.Arguments != null)
                {
                    foreach (Argument arg in declMethod.Arguments)
                    {
                        if (arg.Direction == Introspection.ArgDirection.@in)
                        {
                            parms.Add(new Signature(arg.Type).ToType());
                        }
                        //if (arg.Direction == Introspection.ArgDirection.@out)
                        //	parms.Add (new Signature (arg.Type).ToType ().MakeByRefType ());
                    }
                }

                Signature outSig = Signature.Empty;
                //this just takes the last out arg and uses is as the return type
                if (declMethod.Arguments != null)
                {
                    foreach (Argument arg in declMethod.Arguments)
                    {
                        if (arg.Direction == Introspection.ArgDirection.@out)
                        {
                            outSig = new Signature(arg.Type);
                        }
                    }
                }

                Type retType = outSig == Signature.Empty ? typeof(void) : outSig.ToType();

                MethodBuilder method_builder = typeB.DefineMethod(declMethod.Name, ifaceMethAttr, retType, parms.ToArray());

                //define the parameter attributes and names
                if (declMethod.Arguments != null)
                {
                    int argNum = 0;

                    foreach (Argument arg in declMethod.Arguments)
                    {
                        if (arg.Direction == Introspection.ArgDirection.@in)
                        {
                            method_builder.DefineParameter(++argNum, ParameterAttributes.In, arg.Name);
                        }
                        //if (arg.Direction == Introspection.ArgDirection.@out)
                        //	method_builder.DefineParameter (++argNum, ParameterAttributes.Out, arg.Name);
                    }
                }
            }

            if (iface.Properties != null)
            {
                foreach (NDesk.DBus.Introspection.Property prop in iface.Properties)
                {
                    Type propType = new Signature(prop.Type).ToType();

                    PropertyBuilder prop_builder = typeB.DefineProperty(prop.Name, PropertyAttributes.None, propType, Type.EmptyTypes);

                    if (prop.Access == propertyAccess.read || prop.Access == propertyAccess.readwrite)
                    {
                        prop_builder.SetGetMethod(typeB.DefineMethod("get_" + prop.Name, ifaceMethAttr | MethodAttributes.SpecialName, propType, Type.EmptyTypes));
                    }

                    if (prop.Access == propertyAccess.write || prop.Access == propertyAccess.readwrite)
                    {
                        prop_builder.SetSetMethod(typeB.DefineMethod("set_" + prop.Name, ifaceMethAttr | MethodAttributes.SpecialName, null, new Type[] { propType }));
                    }
                }
            }

            if (iface.Signals != null)
            {
                foreach (NDesk.DBus.Introspection.Signal signal in iface.Signals)
                {
                    //Type eventType = typeof (EventHandler);
                    Type eventType = typeof(VoidHandler);

                    EventBuilder event_builder = typeB.DefineEvent(signal.Name, EventAttributes.None, eventType);

                    event_builder.SetAddOnMethod(typeB.DefineMethod("add_" + signal.Name, ifaceMethAttr | MethodAttributes.SpecialName, null, new Type[] { eventType }));

                    event_builder.SetRemoveOnMethod(typeB.DefineMethod("remove_" + signal.Name, ifaceMethAttr | MethodAttributes.SpecialName, null, new Type[] { eventType }));
                }
            }

            //apply InterfaceAttribute
            ConstructorInfo interfaceAttributeCtor = typeof(InterfaceAttribute).GetConstructor(new Type[] { typeof(string) });

            CustomAttributeBuilder cab = new CustomAttributeBuilder(interfaceAttributeCtor, new object[] { iface.Name });

            typeB.SetCustomAttribute(cab);
        }
        public void GetValue(Type type, out object val)
        {
            if (type == typeof(void))
            {
                val = null;
                return;
            }

            if (type.IsArray)
            {
                Array valArr;
                GetValue(type, out valArr);
                val = valArr;
            }
            else if (type == typeof(ObjectPath))
            {
                ObjectPath valOP;
                GetValue(out valOP);
                val = valOP;
            }
            else if (type == typeof(Signature))
            {
                Signature valSig;
                GetValue(out valSig);
                val = valSig;
            }
            else if (type == typeof(object))
            {
                GetValue(out val);
            }
            else if (type == typeof(string))
            {
                string valStr;
                GetValue(out valStr);
                val = valStr;
            }
            else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IDictionary <,>))
            {
                Type[] genArgs = type.GetGenericArguments();
                //Type dictType = typeof (Dictionary<,>).MakeGenericType (genArgs);
                //workaround for Mono bug #81035 (memory leak)
                Type dictType = Mapper.GetGenericType(typeof(Dictionary <,>), genArgs);
                val = Activator.CreateInstance(dictType, new object[0]);
                System.Collections.IDictionary idict = (System.Collections.IDictionary)val;
                GetValueToDict(genArgs[0], genArgs[1], idict);
            }
            else if (Mapper.IsPublic(type))
            {
                GetObject(type, out val);
            }
            else if (!type.IsPrimitive && !type.IsEnum)
            {
                GetValueStruct(type, out val);
            }
            else
            {
                DType dtype = Signature.TypeToDType(type);
                GetValue(dtype, out val);
            }

            if (type.IsEnum)
            {
                val = Enum.ToObject(type, val);
            }
        }
 public void GetValue(Signature sig, out object val)
 {
     GetValue(sig.ToType(), out val);
 }
Ejemplo n.º 4
0
 object ReadVariant(Signature sig)
 {
     return(ReadValue(sig.ToType()));
 }