Exemple #1
0
        /// <inheritdoc />
        public IDynamicallyGeneratedMethodData StartMethod(string methodName, Type returnedValueType, IEnumerable <IMethodParameterInfo> parametersData, AccessLevel accessLevel, bool isStatic, bool addUniqueIdPostfix)
        {
            if (addUniqueIdPostfix)
            {
                methodName = $"{methodName}_{GlobalsCoreAmbientContext.Context.GenerateUniqueId()}";
            }

            IDynamicallyGeneratedMethodData dynamicallyGeneratedMethodData = new DynamicallyGeneratedMethodData(methodName);

            _inProgressMethodsData.Add(dynamicallyGeneratedMethodData);

            dynamicallyGeneratedMethodData.AddCode(GetAccessLevel(accessLevel));
            dynamicallyGeneratedMethodData.AddCode(" ");

            if (isStatic)
            {
                dynamicallyGeneratedMethodData.AddCode("static ");
            }

            if (returnedValueType == typeof(void))
            {
                dynamicallyGeneratedMethodData.AddCode("void");
            }
            else
            {
                dynamicallyGeneratedMethodData.AddCode(returnedValueType.GetTypeNameInCSharpClass());
            }

            dynamicallyGeneratedMethodData.AddCode(" ");
            dynamicallyGeneratedMethodData.AddCode(methodName);

            AddMethodSignature(dynamicallyGeneratedMethodData, parametersData);
            return(dynamicallyGeneratedMethodData);
        }
Exemple #2
0
        /// <inheritdoc />
        public IDynamicallyGeneratedMethodData StartInterfaceImplementationMethod(MethodInfo methodInfo, bool isExplicitMethod)
        {
            if (methodInfo.IsStatic)
            {
                throw new ArgumentException($"Method '{methodInfo.Name}' cannot be a static method.", nameof(methodInfo));
            }

            if (!methodInfo.DeclaringType.IsInterface)
            {
                throw new ArgumentException($"Method '{methodInfo.Name}' should be an interface method.", nameof(methodInfo));
            }

            if (methodInfo.IsAssembly)
            {
                throw new ArgumentException($"Method '{methodInfo.Name}' cannot be overridden or implemented, since it has 'internal' visibility.", nameof(methodInfo));
            }

            string methodName;

            if (isExplicitMethod)
            {
                methodName = $"{methodInfo.DeclaringType.GetTypeNameInCSharpClass()}.{methodInfo.Name}";
            }
            else
            {
                methodName = methodInfo.Name;
            }

            IDynamicallyGeneratedMethodData dynamicallyGeneratedMethodData = new DynamicallyGeneratedMethodData(methodName);

            _inProgressMethodsData.Add(dynamicallyGeneratedMethodData);

            if (!isExplicitMethod)
            {
                dynamicallyGeneratedMethodData.AddCode("public ");
            }


            if (methodInfo.ReturnType == typeof(void))
            {
                dynamicallyGeneratedMethodData.AddCode("void");
            }
            else
            {
                dynamicallyGeneratedMethodData.AddCode(methodInfo.ReturnType.GetTypeNameInCSharpClass());
            }

            dynamicallyGeneratedMethodData.AddCode(" ");

            dynamicallyGeneratedMethodData.AddCode(methodName);

            AddOverriddenOrImplementedMethodSignature(dynamicallyGeneratedMethodData, methodInfo);

            return(dynamicallyGeneratedMethodData);
        }
Exemple #3
0
        /// <inheritdoc />
        public IDynamicallyGeneratedMethodData StartOverrideMethod(MethodInfo methodInfo)
        {
            if (methodInfo.IsStatic)
            {
                throw new ArgumentException($"Method '{methodInfo.Name}' cannot be a static method.", nameof(methodInfo));
            }

            if (methodInfo.IsAssembly)
            {
                throw new ArgumentException($"Method '{methodInfo.Name}' cannot be overridden or implemented, since it has 'internal' visibility.", nameof(methodInfo));
            }

            if (methodInfo.DeclaringType.IsInterface)
            {
                throw new ArgumentException($"Method '{methodInfo.Name}' should not be an interface method.", nameof(methodInfo));
            }

            if (methodInfo.IsFinal)
            {
                throw new ArgumentException($"Method '{methodInfo.Name}' is a final method and cannot be overridden.", nameof(methodInfo));
            }

            if (methodInfo.IsPrivate)
            {
                throw new ArgumentException($"Method '{methodInfo.Name}' should have either public or protected visibility.", nameof(methodInfo));
            }

            if (!methodInfo.IsVirtual)
            {
                throw new ArgumentException($"Method '{methodInfo.Name}' should be a virtual method.", nameof(methodInfo));
            }

            IDynamicallyGeneratedMethodData dynamicallyGeneratedMethodData = new DynamicallyGeneratedMethodData(methodInfo.Name);

            _inProgressMethodsData.Add(dynamicallyGeneratedMethodData);

            if (methodInfo.IsPublic)
            {
                dynamicallyGeneratedMethodData.AddCode("public");
            }
            else
            {
                dynamicallyGeneratedMethodData.AddCode("protected");
            }


            dynamicallyGeneratedMethodData.AddCode(" override");

            if (methodInfo.ReturnType == typeof(void))
            {
                dynamicallyGeneratedMethodData.AddCode("void");
            }
            else
            {
                dynamicallyGeneratedMethodData.AddCode(methodInfo.ReturnType.GetTypeNameInCSharpClass());
            }

            dynamicallyGeneratedMethodData.AddCode(" ");


            dynamicallyGeneratedMethodData.AddCode(methodInfo.Name);

            AddOverriddenOrImplementedMethodSignature(dynamicallyGeneratedMethodData, methodInfo);

            return(dynamicallyGeneratedMethodData);
        }