Example #1
0
        public void Build(Type type)
        {
            if (!type.IsInterface)
            {
                return;
            }

            var ghostClassCode = _BuildGhostCode(type);

            var typeName = _GetTypeName(type);

            GpiEvent?.Invoke(typeName, ghostClassCode);

            foreach (var eventInfo in type.GetEvents())
            {
                var eventCode = _BuildEventCode(type, eventInfo);

                OnEventEvent?.Invoke(typeName, eventInfo.Name, eventCode);
            }
        }
Example #2
0
        public void Build(string protocol_name, Type[] types)
        {
            var codeGpis   = new List <string>();
            var codeEvents = new List <string>();


            var addGhostType = new List <string>();
            var addEventType = new List <string>();

            var serializerTypes = new HashSet <Type>();

            var memberMapMethodBuilder   = new List <string>();
            var memberMapEventBuilder    = new List <string>();
            var memberMapPropertyBuilder = new List <string>();

            var memberMapInterfaceBuilder = new List <string>();

            foreach (var type in types)
            {
                serializerTypes.Add(type);

                if (!type.IsInterface)
                {
                    continue;
                }

                var ghostClassCode = _BuildGhostCode(type);

                var typeName = _GetTypeName(type);

                addGhostType.Add($"types.Add(typeof({typeName}) , typeof({_GetGhostType(type)}) );");

                codeGpis.Add(ghostClassCode);

                OnGpiEvent?.Invoke(typeName, ghostClassCode);

                // build event
                foreach (var eventInfo in type.GetEvents())
                {
                    addEventType.Add($"eventClosures.Add(new {_GetEventType(type, eventInfo.Name)}() );");

                    var eventCode = _BuildEventCode(type, eventInfo);

                    codeEvents.Add(eventCode);

                    OnEventEvent?.Invoke(typeName, eventInfo.Name, eventCode);

                    memberMapEventBuilder.Add($"typeof({type.FullName}).GetEvent(\"{eventInfo.Name}\")");
                }

                // build method
                var methodInfos = type.GetMethods();

                foreach (var methodInfo in methodInfos)
                {
                    if (methodInfo.IsPublic && methodInfo.IsSpecialName == false)
                    {
                        foreach (var parameterInfo in methodInfo.GetParameters())
                        {
                            serializerTypes.Add(parameterInfo.ParameterType);
                        }

                        memberMapMethodBuilder.Add($"typeof({type.FullName}).GetMethod(\"{methodInfo.Name}\")");
                    }
                }

                // build property
                var propertyInfos = type.GetProperties();

                foreach (var propertyInfo in propertyInfos)
                {
                    memberMapPropertyBuilder.Add($"typeof({type.FullName}).GetProperty(\"{propertyInfo.Name}\")");
                }

                memberMapInterfaceBuilder.Add($"typeof({type.FullName})");
            }

            var addMemberMapInterfaceCode = string.Join(",", memberMapInterfaceBuilder.ToArray());
            var addMemberMapPropertyCode  = string.Join(",", memberMapPropertyBuilder.ToArray());
            var addMemberMapEventCode     = string.Join(",", memberMapEventBuilder.ToArray());
            var addMemberMapMethodCode    = string.Join(",", memberMapMethodBuilder.ToArray());
            var addTypeCode      = string.Join("\n", addGhostType.ToArray());
            var addDescriberCode = string.Join(",", _GetSerializarType(serializerTypes));
            var addEventCode     = string.Join("\n", addEventType.ToArray());
            var tokens           = protocol_name.Split(new[] { '.' });
            var protocolName     = tokens.Last();

            var providerNamespace = string.Join(".", tokens.Take(tokens.Count() - 1).ToArray());

            var providerNamespaceHead = string.Empty;

            var providerNamespaceTail = string.Empty;

            if (string.IsNullOrEmpty(providerNamespace) == false)
            {
                providerNamespaceHead = $"namespace {providerNamespace}{{ ";
                providerNamespaceTail = "}";
            }

            var builder = new StringBuilder();

            builder.Append(addTypeCode);
            builder.Append(addEventCode);
            builder.Append(addDescriberCode);

            var verificationCode = _BuildVerificationCode(builder);

            var providerCode =
                $@"
            using System;  
            using System.Collections.Generic;
            
            {providerNamespaceHead}
                public class {protocolName} : Synchronization.PreGenerated.IProtocol
                {{
                    Synchronization.PreGenerated.InterfaceProvider _InterfaceProvider;
                    Synchronization.PreGenerated.EventProvider _EventProvider;
                    Synchronization.PreGenerated.MemberMap _MemberMap;
                    Gateway.Serialization.ISerializer _Serializer;
                    public {protocolName}()
                    {{
                        var types = new Dictionary<Type, Type>();
                        {addTypeCode}
                        _InterfaceProvider = new Synchronization.PreGenerated.InterfaceProvider(types);

                        var eventClosures = new List<Synchronization.Interface.IEventProxyCreator>();
                        {addEventCode}
                        _EventProvider = new  Synchronization.PreGenerated.EventProvider(eventClosures);

                        _Serializer = new Serialization.Serializer(new Serialization.DescriberBuilder({addDescriberCode}));


                        _MemberMap = new Synchronization.PreGenerated.MemberMap(new System.Reflection.MethodInfo[] {{{addMemberMapMethodCode}}} ,new System.Reflection.EventInfo[]{{ {addMemberMapEventCode} }}, new System.Reflection.PropertyInfo[] {{{addMemberMapPropertyCode} }}, new Type[] {{{addMemberMapInterfaceCode}}});
                    }}

                    byte[] Synchronization.PreGenerated.IProtocol.VerificationCode {{ get {{ return new byte[]{{{verificationCode}}};}} }}
                    Synchronization.PreGenerated.InterfaceProvider Synchronization.PreGenerated.IProtocol.GetInterfaceProvider()
                    {{
                        return _InterfaceProvider;
                    }}

                    Synchronization.PreGenerated.EventProvider Synchronization.PreGenerated.IProtocol.GetEventProvider()
                    {{
                        return _EventProvider;
                    }}

                    Gateway.Serialization.ISerializer Synchronization.PreGenerated.IProtocol.GetSerialize()
                    {{
                        return _Serializer;
                    }}

                    Synchronization.PreGenerated.MemberMap Synchronization.PreGenerated.IProtocol.GetMemberMap()
                    {{
                        return _MemberMap;
                    }}
                    
                }}
            {providerNamespaceTail}
            ";

            OnProviderEvent?.Invoke(protocol_name, providerCode);
        }