Beispiel #1
0
        private void ConvertGetter(MethodInfo getter)
        {
            var retType      = getter.ReturnType;
            var propertyName = getter.Name.GetPropertyName();
            var method       = CodeDomEx.CreateMethod(getter.Name.GetNormalMethodName(), retType);

            if (Wrapped.IsRegMember(RegPairs, out var regPair))
            {
                CodeFieldReferenceExpression regRef;
                if (!TryGetReferenceToFieldOfType(regPair.Registry, out regRef))
                {
                    regRef = AddConstructorInitializedField(regPair.Registry);
                }

                var localInstanceRef  = GetRegistryMemberFromRegistryField(method, regPair, regRef);
                var propertyReference = new CodePropertyReferenceExpression(localInstanceRef, propertyName);
                ConvertReturnedProperty(method, retType, propertyReference);
            }
            else
            {
                var wrapPropRef = new CodePropertyReferenceExpression(WrappedRef, propertyName);
                ConvertReturnedProperty(method, retType, wrapPropRef);
            }

            Klass.Members.Add(method);
        }
Beispiel #2
0
        public Converter WithEvents()
        {
            // TODO:
            if (AddedEvents || Wrapped.IsRegMember(RegPairs, out _))
            {
                return(this);
            }

            foreach (var e in Wrapped.GetEvents())
            {
                var eventName = e.Name;
                var eventType = e.EventHandlerType
                                .GetGenericArguments()
                                .FirstOrDefault();

                // Creates an Action event that doesn't require ActionProxy when subscribing
                var newEvent = CreateEvent(eventName);

                // Create method a which gets called when the Action event fires, and forwards the event to the normal C# event
                string           methodName = "Forward" + eventName + "Event";
                CodeMemberMethod newMethod  = CodeDomEx.CreateMethod(methodName, typeof(void));

                if (eventType == null)
                {
                    AddVoidActionEvent(newEvent, newMethod);
                }
                else
                {
                    AddActionEventWithRetVal(newEvent, eventType, newMethod);
                }

                Klass.Members.Add(newEvent);
                Klass.Members.Add(newMethod);

                // Add statements to the constructor to invoke the new event when the event in the
                // wrapped object fires.

                var actionProxyType = eventType == null
                  ? "SuperMemoAssistant.Sys.Remoting.ActionProxy"
                  : $"SuperMemoAssistant.Sys.Remoting.ActionProxy<{eventType.FullName}>";

                CodeDelegateCreateExpression createDelegate1 = new CodeDelegateCreateExpression(
                    new CodeTypeReference(actionProxyType), new CodeThisReferenceExpression(), methodName);

                CodeEventReferenceExpression actionEventRef = new CodeEventReferenceExpression(
                    WrappedRef, eventName);

                // Attaches an EventHandler delegate pointing to TestMethod to the TestEvent event.
                CodeAttachEventStatement attachStatement1 = new CodeAttachEventStatement(actionEventRef, createDelegate1);

                Constructor.Statements.Add(attachStatement1);
            }
            AddedEvents = true;
            return(this);
        }
Beispiel #3
0
        private void ConvertMethod(MethodInfo info)
        {
            var method = CodeDomEx.CreateMethod(info.Name, info.ReturnType);

            method.Comments.Add(new CodeCommentStatement(GetDocsFor(info)));

            var targetMethodRef  = new CodeMethodReferenceExpression(WrappedRef, info.Name);
            var targetMethodArgs = new List <CodeExpression>();
            var retType          = info.ReturnType;

            if (Wrapped.IsRegMember(RegPairs, out var regPair))
            {
                CodeFieldReferenceExpression regRef;
                if (!TryGetReferenceToFieldOfType(regPair.Registry, out regRef))
                {
                    regRef = AddConstructorInitializedField(regPair.Registry);
                }

                var localInstanceRef = GetRegistryMemberFromRegistryField(method, regPair, regRef);
                targetMethodRef = new CodeMethodReferenceExpression(localInstanceRef, info.Name);
            }

            var parameters = info.GetParameters();

            if (parameters != null)
            {
                foreach (var parameter in parameters)
                {
                    if (parameter.IsOut)
                    {
                        var paramType = parameter.ParameterType;
                        var decl      = new CodeVariableDeclarationStatement(new CodeTypeReference(paramType.GetElementType()), Namer.GetName());
                        method.Statements.Add(decl);
                        targetMethodArgs.Add(new CodeSnippetExpression("out " + decl.Name));
                        continue;
                    }

                    if (parameter.ParameterType.IsRegMember(RegPairs, out _))
                    {
                        string x = ConvertParameter(parameter, method);
                        targetMethodArgs.Add(new CodeVariableReferenceExpression(x));
                    }
                    else
                    {
                        string x = KeepParameter(parameter, method);
                        targetMethodArgs.Add(new CodeArgumentReferenceExpression(x));
                    }
                }
            }

            var invoke = new CodeMethodInvokeExpression(targetMethodRef, targetMethodArgs.ToArray());

            ConvertReturnedInvoke(method, retType, invoke);
            Klass.Members.Add(method);
        }
Beispiel #4
0
        private void ConvertSetter(MethodInfo setter)
        {
            var propertyName = setter.Name.GetPropertyName();
            var method       = CodeDomEx.CreateMethod(setter.Name.GetNormalMethodName(), typeof(void));

            CodePropertyReferenceExpression targetProp =
                new CodePropertyReferenceExpression(WrappedRef, propertyName);

            if (Wrapped.IsRegMember(RegPairs, out var regPair))
            {
                CodeFieldReferenceExpression regRef;
                if (!TryGetReferenceToFieldOfType(regPair.Registry, out regRef))
                {
                    regRef = AddConstructorInitializedField(regPair.Registry);
                }

                var localInstanceRef = GetRegistryMemberFromRegistryField(method, regPair, regRef);
                targetProp = new CodePropertyReferenceExpression(new CodeVariableReferenceExpression(localInstanceRef.VariableName), propertyName);
            }

            var info      = setter.GetParameters()[0];
            var paramType = info.ParameterType;

            if (paramType.IsRegMember(RegPairs, out _))
            {
                string x = ConvertParameter(info, method);
                method.Statements.Add(new CodeAssignStatement(targetProp, new CodeVariableReferenceExpression(x)));
            }
            else
            {
                string x = KeepParameter(info, method);
                method.Statements.Add(new CodeAssignStatement(targetProp, new CodeArgumentReferenceExpression(x)));
            }

            Klass.Members.Add(method);
        }