Exemple #1
0
        public Type Build <TPackage>(string assembly, string classname, string packageType)
            where TPackage : new()
        {
            var str = String.Format(@"using System; using Lidgren.Network; using Gem.Network.Extensions; using {3};
                                            namespace Gem.Network.Builders
                                            {{
                                            public class {0} : Gem.Network.Handlers.IMessageHandler
                                             {{ private readonly Action<NetConnection,{2}> action;
                                              public {0}() {{}}                                                                                               
                                                 public {0}(Action<NetConnection,{2}> action)
                                                 {{
                                                     this.action = action;
                                                 }}
                                                  public void Handle(NetConnection sender, object args)
                                                 {{
                                                      var props = args.ReadAllProperties();
                                                      {2} package = new {2} {{ {1} }};
                                                      action(sender, package);
                                                 }}                                           
                                                }}                                                                                        
                                             }}", classname,
                                    GetMapping(RuntimePropertyInfo.GetPropertyTypesAndNames <TPackage>().ToList()),
                                    packageType,
                                    assembly);

            return(CSScript.LoadCode(str)
                   .CreateObject("*")
                   .AlignToInterface <IMessageHandler>()
                   .GetType());
        }
        static PropertyInfo GetBasePropertyDefinition(RuntimePropertyInfo property)
        {
            MethodInfo method = property.GetGetMethod(true);

            if (method == null || !method.IsVirtual)
            {
                method = property.GetSetMethod(true);
            }
            if (method == null || !method.IsVirtual)
            {
                return(null);
            }

            MethodInfo baseMethod = ((RuntimeMethodInfo)method).GetBaseMethod();

            if (baseMethod != null && baseMethod != method)
            {
                ParameterInfo[] parameters = property.GetIndexParameters();
                if (parameters != null && parameters.Length > 0)
                {
                    Type[] paramTypes = new Type[parameters.Length];
                    for (int i = 0; i < paramTypes.Length; i++)
                    {
                        paramTypes[i] = parameters[i].ParameterType;
                    }
                    return(baseMethod.DeclaringType.GetProperty(property.Name, property.PropertyType,
                                                                paramTypes));
                }
                else
                {
                    return(baseMethod.DeclaringType.GetProperty(property.Name, property.PropertyType));
                }
            }
            return(null);
        }
Exemple #3
0
        RuntimePropertyInfo[] GetPropertiesByName(string name, BindingFlags bindingAttr, MemberListType listType, RuntimeType reflectedType)
        {
            var refh = new RuntimeTypeHandle(reflectedType);

            using (var namePtr = new Mono.SafeStringMarshal(name))
                using (var h = new Mono.SafeGPtrArrayHandle(GetPropertiesByName_native(namePtr.Value, bindingAttr, listType))) {
                    var n = h.Length;
                    var a = new RuntimePropertyInfo [n];
                    for (int i = 0; i < n; i++)
                    {
                        var ph = new Mono.RuntimePropertyHandle(h[i]);
                        a[i] = (RuntimePropertyInfo)RuntimePropertyInfo.GetPropertyFromHandle(ph, refh);
                    }
                    return(a);
                }
        }
Exemple #4
0
        internal IEnumerable <PropertyInfo> CoreGetDeclaredProperties(NameFilter optionalNameFilter, RuntimeTypeInfo reflectedType)
        {
            RuntimeNamedTypeInfo definingType = AnchoringTypeDefinitionForDeclaredMembers;

            if (definingType != null)
            {
                MetadataReader reader = definingType.Reader;
                foreach (PropertyHandle propertyHandle in definingType.DeclaredPropertyHandles)
                {
                    if (optionalNameFilter == null || optionalNameFilter.Matches(propertyHandle.GetProperty(reader).Name, reader))
                    {
                        yield return(RuntimePropertyInfo.GetRuntimePropertyInfo(propertyHandle, definingType, this, reflectedType));
                    }
                }
            }
        }
Exemple #5
0
        private RuntimePropertyInfo LookupDeclaredPropertyByName(String name)
        {
            RuntimeNamedTypeInfo definingType         = _runtimeTypeInfo.AnchoringTypeDefinitionForDeclaredMembers;
            IEnumerator <RuntimePropertyInfo> matches = _runtimeTypeInfo.GetDeclaredPropertiesInternal(definingType, name).GetEnumerator();

            if (!matches.MoveNext())
            {
                return(null);
            }
            RuntimePropertyInfo result = matches.Current;

            if (matches.MoveNext())
            {
                throw new AmbiguousMatchException();
            }
            return(result);
        }
        private void AttachAttributes(RuntimePropertyInfo propertyInfo, ValueEntry entry)
        {
            if (entry.IsString)
            {
                if (entry.Descriptions.Count > 0)
                {
                    if (int.TryParse(entry.Descriptions[0], out int max))
                    {
                        propertyInfo.AddAttribute(new StringLengthAttribute(0, max));
                    }

                    if (entry.TypeName == "ascii")
                    {
                        propertyInfo.AddAttribute(new ASCIIStringAttribute());
                    }
                }
            }
        }
Exemple #7
0
        //
        // Return all declared properties whose name matches "optionalNameFilter". If optionalNameFilter is null, return them all.
        //
        internal IEnumerable <RuntimePropertyInfo> GetDeclaredPropertiesInternal(RuntimeNamedTypeInfo definingType, String optionalNameFilter)
        {
            if (definingType != null)
            {
                // We require the caller to pass a value that we could calculate ourselves because we're an iterator and we
                // don't want any MissingMetadataException that AnchoringType throws to be deferred.
                Debug.Assert(definingType.Equals(this.AnchoringTypeDefinitionForDeclaredMembers));

                MetadataReader reader = definingType.Reader;
                foreach (PropertyHandle propertyHandle in definingType.DeclaredPropertyHandles)
                {
                    if (optionalNameFilter == null || propertyHandle.GetProperty(reader).Name.StringEquals(optionalNameFilter, reader))
                    {
                        yield return(RuntimePropertyInfo.GetRuntimePropertyInfo(propertyHandle, definingType, this));
                    }
                }
            }
        }
Exemple #8
0
        public INetworkEvent AndHandleWith<T>(T objectToHandle, Expression<Func<T, Delegate>> methodToHandle)
        {
            var methodInfo = methodToHandle.GetMethodInfo();
            var types = methodInfo.GetParameters().Select(x => x.ParameterType).ToList();

            Guard.That(types.All(x => x.IsPrimitive || x == typeof(string)), "All types should be primitive");

            var properties = RuntimePropertyInfo.GetPropertyInfo(types.ToArray());
            
            SetDynamicPoco(properties);
            SetMessageHandler(properties.Select(x => RuntimePropertyInfo.GetPrimitiveTypeAlias(x.PropertyType)).ToList(), objectToHandle, methodInfo.Name);
            var argumentsDisposable = GemClient.MessageFlow[profile,messageType].Add(messageFlowArgs);
            SetDynamicEvent(argumentsDisposable);

            messageFlowArgs.IncludesLocalTime = false;
            GemClient.MessageFlow[profile, messageType].SubscribeEvent(messageFlowArgs.ID);

            return messageFlowArgs.EventRaisingclass;
        }
Exemple #9
0
        private static byte CreateMessageFlowArguments(byte id, Type type, string profile)
        {
            var properties      = RuntimePropertyInfo.GetPropertyInfo(Activator.CreateInstance(type).GetPropertyTypes().ToArray());
            var messageFlowArgs = new MessageFlowArguments();

            messageFlowArgs.MessageHandler = new DummyHandler();
            messageFlowArgs.MessagePoco    = Dependencies.Container.Resolve <IPocoFactory>().Create(properties, "poco" + id);
            messageFlowArgs.ID             = (byte)(GemNetwork.InitialId + ProtocolObjectsFound++);

            GemClient.MessageFlow[profile, MessageType.Data].Add(messageFlowArgs);
            GemServer.MessageFlow[profile, MessageType.Data].Add(new MessageArguments
            {
                ID             = messageFlowArgs.ID,
                MessageHandler = new DummyHandler(),
                MessagePoco    = messageFlowArgs.MessagePoco
            });

            return(messageFlowArgs.ID);
        }
Exemple #10
0
 internal static RuntimePropertyIndexParameterInfo GetRuntimePropertyIndexParameterInfo(RuntimePropertyInfo member, RuntimeParameterInfo backingParameter)
 {
     return(new RuntimePropertyIndexParameterInfo(member, backingParameter));
 }
 internal RemotingCachedData(RuntimePropertyInfo ri)
 {
     this.RI = ri;
 }
 private RuntimePropertyIndexParameterInfo(RuntimePropertyInfo member, RuntimeParameterInfo backingParameter)
     : base(member, backingParameter.Position)
 {
     _backingParameter = backingParameter;
 }
Exemple #13
0
 private RuntimePropertyIndexParameterInfo(RuntimePropertyInfo member, RuntimeParameterInfo backingParameter)
     : base(member, backingParameter.Position)
 {
     _backingParameter = backingParameter;
 }
Exemple #14
0
        private string GetMapping(List <RuntimePropertyInfo> propertyFields)
        {
            var sb = new StringBuilder();

            for (int i = 0; i < propertyFields.Count; i++)
            {
                sb.Append(string.Format("{0} =({1})props[{2}],", propertyFields[i].PropertyName, RuntimePropertyInfo.GetPrimitiveTypeAlias(propertyFields[i].PropertyType), i));
            }
            sb.Length--;
            return(sb.ToString());
        }
Exemple #15
0
        internal static RemotingCachedData GetReflectionCachedData(MemberInfo mi)
        {
            RemotingCachedData     data  = null;
            MethodBase             base2 = null;
            RuntimeType            type  = null;
            RuntimeFieldInfo       ri    = null;
            RuntimeEventInfo       info2 = null;
            RuntimePropertyInfo    info3 = null;
            SerializationFieldInfo info4 = null;

            base2 = mi as MethodBase;
            if (base2 != null)
            {
                return(GetReflectionCachedData(base2));
            }
            type = mi as RuntimeType;
            if (type != null)
            {
                return(GetReflectionCachedData(type));
            }
            ri = mi as RuntimeFieldInfo;
            if (ri != null)
            {
                data = (RemotingCachedData)ri.RemotingCache[CacheObjType.RemotingData];
                if (data == null)
                {
                    ri.RemotingCache[CacheObjType.RemotingData] = data = new RemotingCachedData(ri);
                }
                return(data);
            }
            info2 = mi as RuntimeEventInfo;
            if (info2 != null)
            {
                data = (RemotingCachedData)info2.RemotingCache[CacheObjType.RemotingData];
                if (data == null)
                {
                    info2.RemotingCache[CacheObjType.RemotingData] = data = new RemotingCachedData(info2);
                }
                return(data);
            }
            info3 = mi as RuntimePropertyInfo;
            if (info3 != null)
            {
                data = (RemotingCachedData)info3.RemotingCache[CacheObjType.RemotingData];
                if (data == null)
                {
                    info3.RemotingCache[CacheObjType.RemotingData] = data = new RemotingCachedData(info3);
                }
                return(data);
            }
            info4 = mi as SerializationFieldInfo;
            if (info4 == null)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeReflectionObject"));
            }
            data = (RemotingCachedData)info4.RemotingCache[CacheObjType.RemotingData];
            if (data == null)
            {
                info4.RemotingCache[CacheObjType.RemotingData] = data = new RemotingCachedData(info4);
            }
            return(data);
        }