Example #1
0
        public Type CreateType <TInterface>() where TInterface : class
        {
            if (!typeof(TInterface).IsInterface)
            {
                throw new ArgumentException("Only interfaces are supported.");
            }

            if (typeof(TInterface).GetGenericArguments().Length > 0)
            {
                throw new ArgumentException("Generic interfaces are not supported.");
            }

            if (!ReflectionSerializerVerifier.IsRpcAttributeDefined(typeof(TInterface)))
            {
                throw new ArgumentException($"Unable to create proxy for {typeof(TInterface)}. No {nameof(RdRpcAttribute)} specified.");
            }

            var moduleBuilder = myModuleBuilder.Value;
            var className     = typeof(TInterface).Name.Substring(1);
            var proxyTypeName = "Proxy." + className;
            var typebuilder   = moduleBuilder.DefineType(
                proxyTypeName,
                TypeAttributes.NotPublic | TypeAttributes.Class | TypeAttributes.Sealed,
                typeof(RdExtReflectionBindableBase));

            // Implement interface
            typebuilder.AddInterfaceImplementation(typeof(TInterface));

            // mark it as proxy type
            typebuilder.AddInterfaceImplementation(typeof(IProxyTypeMarker));

            // Add RdExt attribute to type
            var rdExtConstructor = typeof(RdExtAttribute).GetConstructors()[0];

            typebuilder.SetCustomAttribute(new CustomAttributeBuilder(rdExtConstructor, new object[0]));

            var memberNames = new HashSet <string>(StringComparer.Ordinal);
            var members     = typeof(TInterface).GetMembers(BindingFlags.Instance | BindingFlags.Public);

            foreach (var member in members)
            {
                if (!memberNames.Add(member.Name))
                {
                    throw new ArgumentException($"Duplicate member name: {member.Name}. Method overloads are not supported.");
                }
                ImplementMember <TInterface>(typebuilder, member);
            }
#if NET35
            return(typebuilder.CreateType());
#else
            return(typebuilder.CreateTypeInfo());
#endif
        }
Example #2
0
        public static Type GetRpcInterface(TypeInfo typeInfo)
        {
            foreach (var @interface in typeInfo.GetInterfaces())
            {
                if (ReflectionSerializerVerifier.IsRpcAttributeDefined(@interface))
                {
                    return(@interface);
                }
            }

            return(null);
        }
Example #3
0
        public static Type GetRpcInterface(TypeInfo typeInfo)
        {
            if (typeInfo.GetCustomAttribute <RdExtAttribute>() is RdExtAttribute rdExt && rdExt.RdRpcInterface != null)
            {
                return(rdExt.RdRpcInterface);
            }

            foreach (var @interface in typeInfo.GetInterfaces())
            {
                if (ReflectionSerializerVerifier.IsRpcAttributeDefined(@interface))
                {
                    return(@interface);
                }
            }

            return(null);
        }