Ejemplo n.º 1
0
 public abstract void InjectCode(ClassBuilder ownerClass, MethodBuilder method);
Ejemplo n.º 2
0
        private ClassBuilder BuildProxyClassSource(Type superType)
        {
            // build source code for sub-type which inherits superType
            #region Validate
            if (superType.IsSealed)
            {
                throw new ApplicationException(
                          string.Format(
                              "Could not generate proxy type as specified sub-type '{0}' is was declared sealed.",
                              superType.Name
                              )
                          );
            }
            #endregion

            ClassBuilder classBuilder = new ClassBuilder();
            classBuilder.Namespace = superType.Namespace;
            classBuilder.Imports.Add("System.Collections.Generic");
            classBuilder.Imports.Add("DocCube.DataAccessLayer");
            classBuilder.Imports.Add("DocCube.SystemServices");
            classBuilder.ProtectionLevel = ProtectionLevel.Public;
            classBuilder.Name            = string.Format("_{0}Proxy", superType.Name);
            classBuilder.BaseTypeName    = superType.Name;

            // get all methods
            foreach (MethodInfo method in superType.GetMethods())
            {
                if (ContainsProxyAttributes(method))
                {
                    #region Validate

                    if (method.IsStatic)
                    {
                        throw new ApplicationException(
                                  string.Format(
                                      "Could not generate proxy type for sub-type '{0}' as proxyable method '{1}' was declared static.",
                                      superType.Name,
                                      method.Name
                                      )
                                  );
                    }

                    if (method.IsAbstract)
                    {
                        throw new ApplicationException(
                                  string.Format(
                                      "Could not generate proxy type for sub-type '{0}' as proxyable method '{1}' was declared abstract.",
                                      superType.Name,
                                      method.Name
                                      )
                                  );
                    }

                    if (method.IsPrivate)
                    {
                        throw new ApplicationException(
                                  string.Format(
                                      "Could not generate proxy type for sub-type '{0}' as proxyable method '{1}' was declared private.",
                                      superType.Name,
                                      method.Name
                                      )
                                  );
                    }

                    if (!method.IsVirtual)
                    {
                        throw new ApplicationException(
                                  string.Format(
                                      "Could not generate proxy type for sub-type '{0}' as proxyable method '{1}' was not declared virtual.",
                                      superType.Name,
                                      method.Name
                                      )
                                  );
                    }

                    if (method.IsGenericMethod)
                    {
                        throw new ApplicationException(
                                  string.Format(
                                      "Could not generate proxy type for sub-type '{0}' as proxyable method '{1}' was generic.",
                                      superType.Name,
                                      method.Name
                                      )
                                  );
                    }

                    if (method.IsGenericMethodDefinition)
                    {
                        throw new ApplicationException(
                                  string.Format(
                                      "Could not generate proxy type for sub-type '{0}' as proxyable method '{1}' was a generic definition.",
                                      superType.Name,
                                      method.Name
                                      )
                                  );
                    }

                    #endregion

                    MethodBuilder methodBuilder = classBuilder.AppendMethod();
                    methodBuilder.PolymorphicLevel = PolymorphicLevel.Override;
                    methodBuilder.ProtectionLevel  = DetermineProtectionLevel(method);
                    methodBuilder.ReturnTypeName   = method.ReturnParameter.ParameterType.FullName;
                    methodBuilder.Name             = method.Name;

                    // build method parameters
                    foreach (ParameterInfo parameter in method.GetParameters())
                    {
                        ParameterBuilder paramBuilder = methodBuilder.AppendParameter();
                        paramBuilder.ParameterName = parameter.Name;
                        paramBuilder.TypeName      = parameter.ParameterType.FullName;
                    }

                    // if no actual implementation code of the method (i.e. what returns a value) was
                    // injected, then simply return the base class implementation
                    if (!ContainsMethodImplementationAttribute(method))
                    {
                        ReturnBaseImpl attr = new ReturnBaseImpl();
                        attr.InjectCode(classBuilder, methodBuilder);
                    }

                    // inject decorator snippets
                    foreach (CodeBuilderAttribute attr in GetProxyAttributes(method))
                    {
                        attr.InjectCode(classBuilder, methodBuilder);
                    }
                }
            }
            return(classBuilder);
        }