public PropertyNotificationAssets(ModuleDeclaration module)
        {
            if (module == null) throw new ArgumentNullException("module");
            Contract.EndContractBlock();
            //INotifyPropertyChanged Related
            INotifyPropertyChangedTypeSignature = module.FindType(typeof(INotifyPropertyChanged));
            PropertyChangedEventHandlerTypeSignature =
                module.FindType(typeof(INotifyPropertyChanged).GetEvent("PropertyChanged").EventHandlerType);

            PropertyChangedEventHandlerInvokeMethod =
                module.FindMethod(PropertyChangedEventHandlerTypeSignature, "Invoke");

            PropertyChangedEventArgsTypeSignature =
                module.FindType(typeof(PropertyChangedEventArgs));

            PropertyChangedEventArgsConstructor =
                module.FindMethod(PropertyChangedEventArgsTypeSignature, ".ctor");

            //INotifyPropertyChanging Realted
            INotifyPropertyChangingTypeSignature =
                module.FindType(typeof(INotifyPropertyChanging));
            PropertyChangingEventHandlerTypeSignature =
                module.FindType(typeof(INotifyPropertyChanging).GetEvent("PropertyChanging").EventHandlerType);

            PropertyChangingEventHandlerInvokeMethod =
                module.FindMethod(PropertyChangingEventHandlerTypeSignature, "Invoke");

            PropertyChangingEventArgsTypeSignature =
                module.FindType(typeof(PropertyChangingEventArgs));

            PropertyChangingEventArgsConstructor =
                module.FindMethod(PropertyChangingEventArgsTypeSignature, ".ctor");
        }
        public PostEdgeWeaverAssets(ModuleDeclaration module)
        {
            if (module == null) throw new ArgumentNullException("module");
            _module = module;
            _delegateCombineMethod =
                new Lazy<IMethod>(() => {
                    var method = _module.FindMethod("System.Delegate, mscorlib", "Combine", "System.Delegate, mscorlib","System.Delegate, mscorlib");
                    Debug.Assert(method != null, "Could not find Delegate.Combine used by PostEdgeWeaverAssets.");
                    return method;
                });

            _delegateRemoveMethod =
                new Lazy<IMethod>(() => {
                    var method = _module.FindMethod("System.Delegate, mscorlib", "Remove", "System.Delegate, mscorlib", "System.Delegate, mscorlib");
                    Debug.Assert(method != null, "Could not find Delegate.Remove used by PostEdgeWeaverAssets.");
                    return method;
                });

            _compareExchangeMethod =
                new Lazy<IMethod>(() => {
                    var method = _module.FindMethod("System.Threading.Interlocked, mscorlib", "CompareExchange",
                        methodDef => methodDef.IsGenericDefinition);
                    Debug.Assert(method != null, "System.Threading.Interlocked.CompareExchange used by PostEdgeWeaverAssets.");
                    return method;
                });
        }
        public StringFormatWriter(ModuleDeclaration module)
        {
            this.module = module;
            ITypeSignature stringType = module.Cache.GetType(typeof(string));

            this.format1Method = module.FindMethod(stringType, "Format",
                                                   method => method.Parameters.Count == 2 &&
                                                   IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                                                   IntrinsicTypeSignature.Is(method.Parameters[1].ParameterType, IntrinsicType.Object));

            this.format2Method = module.FindMethod(stringType, "Format",
                                                   method => method.Parameters.Count == 3 &&
                                                   IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                                                   IntrinsicTypeSignature.Is(method.Parameters[1].ParameterType, IntrinsicType.Object) &&
                                                   IntrinsicTypeSignature.Is(method.Parameters[2].ParameterType, IntrinsicType.Object));

            this.format3Method = module.FindMethod(stringType, "Format",
                                                   method => method.Parameters.Count == 4 &&
                                                   IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                                                   IntrinsicTypeSignature.Is(method.Parameters[1].ParameterType, IntrinsicType.Object) &&
                                                   IntrinsicTypeSignature.Is(method.Parameters[2].ParameterType, IntrinsicType.Object) &&
                                                   IntrinsicTypeSignature.Is(method.Parameters[3].ParameterType, IntrinsicType.Object));

            this.formatArrayMethod = module.FindMethod(stringType, "Format",
                                                       method => method.Parameters.Count == 2 &&
                                                       IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                                                       method.Parameters[1].ParameterType.BelongsToClassification(TypeClassifications.Array));
        }
Beispiel #4
0
        public static AssemblyLoaderInfo LoadAssemblyLoader(bool createTemporaryAssemblies,
                                                            bool hasUnmanaged,
                                                            ModuleDeclaration module)
        {
            AssemblyLoaderInfo info = new AssemblyLoaderInfo();
            TypeDefDeclaration sourceType;

            if (createTemporaryAssemblies)
            {
                sourceType = module.FindType("PostSharp.Community.Packer.Templates.ILTemplateWithTempAssembly")
                             .GetTypeDefinition();
            }
            else if (hasUnmanaged)
            {
                sourceType = module.FindType("PostSharp.Community.Packer.Templates.ILTemplateWithUnmanagedHandler")
                             .GetTypeDefinition();
            }
            else
            {
                sourceType = module.FindType("PostSharp.Community.Packer.Templates.ILTemplate")
                             .GetTypeDefinition();
            }

            info.AttachMethod            = module.FindMethod(sourceType, "Attach").GetMethodDefinition();
            info.StaticConstructorMethod = module.FindMethod(sourceType, ".cctor").GetMethodDefinition();

            info.AssemblyNamesField = sourceType.FindField("assemblyNames")?.Field;
            info.SymbolNamesField   = sourceType.FindField("symbolNames")?.Field;
            info.PreloadListField   = sourceType.FindField("preloadList")?.Field;
            info.Preload32ListField = sourceType.FindField("preload32List")?.Field;
            info.Preload64ListField = sourceType.FindField("preload64List")?.Field;
            info.Md5HashField       = sourceType.FindField("md5Hash")?.Field;
            info.ChecksumsField     = sourceType.FindField("checksums")?.Field;
            return(info);
        }
        public StringFormatWriter(ModuleDeclaration module)
        {
            this.module = module;
            ITypeSignature stringType = module.Cache.GetType(typeof(string));

            this.format1Method = module.FindMethod(stringType, "Format",
                method => method.Parameters.Count == 2 &&
                          IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                          IntrinsicTypeSignature.Is(method.Parameters[1].ParameterType, IntrinsicType.Object));

            this.format2Method = module.FindMethod(stringType, "Format",
                method => method.Parameters.Count == 3 &&
                          IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                          IntrinsicTypeSignature.Is(method.Parameters[1].ParameterType, IntrinsicType.Object) &&
                          IntrinsicTypeSignature.Is(method.Parameters[2].ParameterType, IntrinsicType.Object));

            this.format3Method = module.FindMethod(stringType, "Format",
                method => method.Parameters.Count == 4 &&
                          IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                          IntrinsicTypeSignature.Is(method.Parameters[1].ParameterType, IntrinsicType.Object) &&
                          IntrinsicTypeSignature.Is(method.Parameters[2].ParameterType, IntrinsicType.Object) &&
                          IntrinsicTypeSignature.Is(method.Parameters[3].ParameterType, IntrinsicType.Object));

            this.formatArrayMethod = module.FindMethod(stringType, "Format",
                method => method.Parameters.Count == 2 &&
                          IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                          method.Parameters[1].ParameterType.BelongsToClassification(TypeClassifications.Array));
        }
 public TransformationAssets(ModuleDeclaration module)
 {
     ObjectTypeSignature = module.FindType(typeof(object));
     ObjectEqualsMethod = module.FindMethod(typeof(object).GetMethod("Equals", new[] { typeof(object), typeof(object) }), BindingOptions.Default);
     LocationBindingTypeSignature = module.FindType(typeof(LocationBinding<>));
     SetValueMethod = module.FindMethod(LocationBindingTypeSignature, "SetValue", x => x.DeclaringType.IsGenericDefinition);
     GetValueMethod = module.FindMethod(LocationBindingTypeSignature, "GetValue", x => x.DeclaringType.IsGenericDefinition);
 }
            // ReSharper restore InconsistentNaming

            public MoveNextTransformation(AsyncStateMachineAspectWeaver parent)
                : base(parent)
            {
                ModuleDeclaration module = parent.Module;

                // ReSharper disable InconsistentNaming
                IType actor_Type = (IType)module.FindType(typeof(Actor), BindingOptions.Default);

                this.actor_GetDispatcher_Method = module.FindMethod(actor_Type, "get_Dispatcher");


                IType dispatcher_Type = (IType)module.FindType(typeof(IDispatcher), BindingOptions.Default);

                this.dispatcher_CheckAccess_Method = module.FindMethod(dispatcher_Type, "CheckAccess");
                this.dispatcher_getSynchronizationContext_Method = module.FindMethod(dispatcher_Type, "get_SynchronizationContext");

                IType task_Type = (IType)module.FindType(typeof(Task), BindingOptions.Default);

                this.task_Yield_Method = module.FindMethod(task_Type, "Yield", 0);

                IType yieldAwaitable_Type = (IType)module.FindType("System.Runtime.CompilerServices.YieldAwaitable, mscorlib", BindingOptions.Default);

                this.yieldAwaitable_GetAwaiter_Method = module.FindMethod(yieldAwaitable_Type, "GetAwaiter");

                this.yieldAwaiter_Type = (IType)this.yieldAwaitable_GetAwaiter_Method.ReturnType;

                this.asyncVoidMethodBuilder_Type = (IType)module.FindType("System.Runtime.CompilerServices.AsyncVoidMethodBuilder, mscorlib",
                                                                          BindingOptions.Default);
                this.asyncVoidMethodBuilder_AwaitUnsafeOnCompleted_Method = module.FindMethod(this.asyncVoidMethodBuilder_Type, "AwaitUnsafeOnCompleted");

                this.asyncTaskMethodBuilder_Type = (IType)module.FindType("System.Runtime.CompilerServices.AsyncTaskMethodBuilder, mscorlib",
                                                                          BindingOptions.Default);
                this.asyncTaskMethodBuilder_AwaitUnsafeOnCompleted_Method = module.FindMethod(this.asyncTaskMethodBuilder_Type, "AwaitUnsafeOnCompleted");

                this.asyncTaskMethodBuilderGenericType = (IType)module.FindType("System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1, mscorlib",
                                                                                BindingOptions.Default);
                this.asyncTaskMethodBuilderGeneric_AwaitUnsafeOnCompleted_Method = module.FindMethod(this.asyncTaskMethodBuilderGenericType,
                                                                                                     "AwaitUnsafeOnCompleted");

                this.synchronizationContext_Type = (IType)module.FindType(typeof(SynchronizationContext), BindingOptions.Default);
                this.synchronizationContext_getCurrent_Method = module.FindMethod(this.synchronizationContext_Type, "get_Current");
                this.synchronizationContext_SetSynchronizationContext_Method = module.FindMethod(this.synchronizationContext_Type, "SetSynchronizationContext");


                // ReSharper restore InconsistentNaming
            }
            public ConsoleCategoryBuilder(ModuleDeclaration module)
            {
                this.module = module;

                this.writeLineMessage = module.FindMethod(
                    module.Cache.GetType(typeof(System.Console)), "WriteLine",
                    method => method.Parameters.Count == 1 &&
                              IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String));

                this.writeLineFormat1 = module.FindMethod(
                    module.Cache.GetType(typeof(System.Console)), "WriteLine",
                    method => method.Parameters.Count == 2 &&
                              IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                              IntrinsicTypeSignature.Is(method.Parameters[1].ParameterType, IntrinsicType.Object));

                this.writeLineFormat2 = module.FindMethod(
                    module.Cache.GetType(typeof(System.Console)), "WriteLine",
                    method => method.Parameters.Count == 3 &&
                              IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                              IntrinsicTypeSignature.Is(method.Parameters[1].ParameterType, IntrinsicType.Object) &&
                              IntrinsicTypeSignature.Is(method.Parameters[2].ParameterType, IntrinsicType.Object));

                this.writeLineFormat3 = module.FindMethod(
                    module.Cache.GetType(typeof(System.Console)), "WriteLine",
                    method => method.Parameters.Count == 4 &&
                              IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                              IntrinsicTypeSignature.Is(method.Parameters[1].ParameterType, IntrinsicType.Object) &&
                              IntrinsicTypeSignature.Is(method.Parameters[2].ParameterType, IntrinsicType.Object) &&
                              IntrinsicTypeSignature.Is(method.Parameters[3].ParameterType, IntrinsicType.Object));

                this.writeLineFormat4 = module.FindMethod(
                    module.Cache.GetType(typeof(System.Console)), "WriteLine",
                    method => method.Parameters.Count == 5 &&
                              IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                              IntrinsicTypeSignature.Is(method.Parameters[1].ParameterType, IntrinsicType.Object) &&
                              IntrinsicTypeSignature.Is(method.Parameters[2].ParameterType, IntrinsicType.Object) &&
                              IntrinsicTypeSignature.Is(method.Parameters[3].ParameterType, IntrinsicType.Object) &&
                              IntrinsicTypeSignature.Is(method.Parameters[4].ParameterType, IntrinsicType.Object));

                this.writeLineFormatArray = module.FindMethod(
                    module.Cache.GetType(typeof(System.Console)), "WriteLine",
                    method => method.Parameters.Count >= 2 &&
                              IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                              method.Parameters[1].ParameterType.BelongsToClassification(TypeClassifications.Array));
            }
        public LoggingImplementationTypeBuilder( ModuleDeclaration module )
        {
            this.module = module;
            this.categoryFields = new Dictionary<string, FieldDefDeclaration>();
            this.wrapperMethods = new Dictionary<IMethod, MethodDefDeclaration>();

            this.stringFormatArrayMethod = module.FindMethod( module.Cache.GetIntrinsic( IntrinsicType.String ), "Format",
                                                              method => method.Parameters.Count == 2 &&
                                                                        IntrinsicTypeSignature.Is( method.Parameters[0].ParameterType, IntrinsicType.String ) &&
                                                                        method.Parameters[1].ParameterType.BelongsToClassification( TypeClassifications.Array ) );

            this.traceWriteLineMethod = module.FindMethod( module.Cache.GetType( typeof(System.Diagnostics.Trace) ), "WriteLine",
                                                           method => method.Parameters.Count == 1 &&
                                                                     IntrinsicTypeSignature.Is( method.Parameters[0].ParameterType, IntrinsicType.String ) );

            this.weavingHelper = new WeavingHelper( module );
            this.implementationType = this.CreateContainingType();
            this.isLoggingField = this.CreateIsLoggingField();
        }
        public LoggingImplementationTypeBuilder(ModuleDeclaration module)
        {
            this.module         = module;
            this.categoryFields = new Dictionary <string, FieldDefDeclaration>();
            this.wrapperMethods = new Dictionary <IMethod, MethodDefDeclaration>();

            this.stringFormatArrayMethod = module.FindMethod(module.Cache.GetIntrinsic(IntrinsicType.String), "Format",
                                                             method => method.Parameters.Count == 2 &&
                                                             IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                                                             method.Parameters[1].ParameterType.BelongsToClassification(TypeClassifications.Array));

            this.traceWriteLineMethod = module.FindMethod(module.Cache.GetType(typeof(System.Diagnostics.Trace)), "WriteLine",
                                                          method => method.Parameters.Count == 1 &&
                                                          IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String));

            this.weavingHelper      = new WeavingHelper(module);
            this.implementationType = this.CreateContainingType();
            this.isLoggingField     = this.CreateIsLoggingField();
        }
        public Assets(ModuleDeclaration module)
        {
            INamedType dictionary = (INamedType)module.FindType(typeof(Dictionary <,>));

            DictionaryOfStringOfStringAdd = module.FindMethod(dictionary, "Add").GetGenericInstance(
                new GenericMap(module, new List <ITypeSignature>
            {
                module.Cache.GetIntrinsic(IntrinsicType.String),
                module.Cache.GetIntrinsic(IntrinsicType.String)
            }));

            INamedType list = (INamedType)module.FindType(typeof(List <>));

            ListOfStringAdd = module.FindMethod(list, "Add").GetGenericInstance(
                new GenericMap(module, new List <ITypeSignature>
            {
                module.Cache.GetIntrinsic(IntrinsicType.String)
            }));
        }
Beispiel #12
0
        public HashCodeInjection(Project project)
        {
            this.project = project;

            // Find Object.GetHashCode():
            ModuleDeclaration module  = this.project.Module;
            INamedType        tObject = module.Cache.GetIntrinsic(IntrinsicType.Object).GetTypeDefinition();

            Object_GetHashCode = module.FindMethod(tObject, "GetHashCode");

            // Find IEnumerator.MoveNext() and IEnumerator.Current:
            IEnumeratorType = (INamedType)module.Cache.GetType(typeof(IEnumerator));
            MoveNext        = module.FindMethod(IEnumeratorType, "MoveNext");
            GetCurrent      = module.FindMethod(IEnumeratorType, "get_Current");

            // Find IEnumerable.GetEnumerator()
            INamedType tEnumerable = (INamedType)module.Cache.GetType(typeof(IEnumerable));

            GetEnumerator = module.FindMethod(tEnumerable, "GetEnumerator");
        }
        private IMethod FindConsoleWriteLine()
        {
            // Represents the module (= assembly) that we're modifying:
            ModuleDeclaration module = this.Project.Module;

            // Finds the System.Console type usable in that module. We don't know exactly where it comes from. It could
            // be mscorlib in .NET Framework or something else in .NET Core:
            INamedType console = (INamedType)module.FindType(typeof(Console));

            // Finds the one overload that we want: System.Console.WriteLine(System.String):
            IGenericMethodDefinition method = module.FindMethod(console, "WriteLine",
                                                                declaration => declaration.Parameters.Count == 1 &&
                                                                declaration.Parameters[0].ParameterType.GetReflectionName() == "System.String");

            return(method);
        }
        public void Initialize(ModuleDeclaration module)
        {
            this.module = module;
            this.loggingImplementation = new LoggingImplementationTypeBuilder(module);
            this.loggerType = module.FindType(typeof(Logger));

            LoggerMethodsBuilder builder = new LoggerMethodsBuilder(module, this.loggerType);

            this.categoryInitializerMethod = module.FindMethod(module.FindType(typeof(LogManager)), "GetLogger",
                method => method.Parameters.Count == 1 && IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) );

            this.loggerMethods[LogLevel.Debug] = builder.CreateLoggerMethods("Trace");
            this.loggerMethods[LogLevel.Info] = builder.CreateLoggerMethods("Info");
            this.loggerMethods[LogLevel.Warning] = builder.CreateLoggerMethods("Warn");
            this.loggerMethods[LogLevel.Error] = builder.CreateLoggerMethods("Error");
            this.loggerMethods[LogLevel.Fatal] = builder.CreateLoggerMethods("Fatal");
        }
        public void Initialize(ModuleDeclaration module)
        {
            this.module = module;
            this.loggingImplementation = new LoggingImplementationTypeBuilder(module);

            this.loggerType = module.FindType(typeof(ILog));
            LoggerMethodsBuilder builder = new LoggerMethodsBuilder(module, this.loggerType);

            this.categoryInitializerMethod = module.FindMethod(module.FindType(typeof(LogManager)), "GetLogger",
                                                               method => method.Parameters.Count == 1 &&
                                                               IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String));

            this.loggerMethods[LogLevel.Debug]   = builder.CreateLoggerMethods("Debug");
            this.loggerMethods[LogLevel.Info]    = builder.CreateLoggerMethods("Info");
            this.loggerMethods[LogLevel.Warning] = builder.CreateLoggerMethods("Warn");
            this.loggerMethods[LogLevel.Error]   = builder.CreateLoggerMethods("Error");
            this.loggerMethods[LogLevel.Fatal]   = builder.CreateLoggerMethods("Fatal");
        }
        private IMethod GetLoggerMethod(ModuleDeclaration module)
        {
            var systemMethod = interceptedMethod.GetSystemMethod(null, null, BindingOptions.Default);
            var systemParameters = systemMethod.GetParameters();

            var parameterTypes = new Type[systemParameters.Length + 1];
            for(int i = 0; i < systemParameters.Length; ++i)
            {
                parameterTypes[i] = systemParameters[i].ParameterType;
            }
            parameterTypes[systemParameters.Length] = typeof(ILog);

            var loggerMethod = Log.LoggerType.GetMethod(systemMethod.Name, BindingFlags.Public | BindingFlags.Static, null, parameterTypes, null);
            if (loggerMethod == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "There is a bug in CodeOMatic.Validation. The method '{0}' does not exist in the Logger class", interceptedMethod));
            }

            return module.FindMethod(loggerMethod, BindingOptions.Default);
        }
Beispiel #17
0
            public ConsoleCategoryBuilder(ConsoleBackend parent, ModuleDeclaration module)
            {
                this.parent = parent;
                this.module = module;

                this.writeLineMessage = module.FindMethod(
                    module.Cache.GetType(typeof(System.Console)), "WriteLine",
                    method => method.Parameters.Count == 1 &&
                    IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String));

                this.writeLineFormat1 = module.FindMethod(
                    module.Cache.GetType(typeof(System.Console)), "WriteLine",
                    method => method.Parameters.Count == 2 &&
                    IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                    IntrinsicTypeSignature.Is(method.Parameters[1].ParameterType, IntrinsicType.Object));

                this.writeLineFormat2 = module.FindMethod(
                    module.Cache.GetType(typeof(System.Console)), "WriteLine",
                    method => method.Parameters.Count == 3 &&
                    IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                    IntrinsicTypeSignature.Is(method.Parameters[1].ParameterType, IntrinsicType.Object) &&
                    IntrinsicTypeSignature.Is(method.Parameters[2].ParameterType, IntrinsicType.Object));

                this.writeLineFormat3 = module.FindMethod(
                    module.Cache.GetType(typeof(System.Console)), "WriteLine",
                    method => method.Parameters.Count == 4 &&
                    IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                    IntrinsicTypeSignature.Is(method.Parameters[1].ParameterType, IntrinsicType.Object) &&
                    IntrinsicTypeSignature.Is(method.Parameters[2].ParameterType, IntrinsicType.Object) &&
                    IntrinsicTypeSignature.Is(method.Parameters[3].ParameterType, IntrinsicType.Object));

                this.writeLineFormat4 = module.FindMethod(
                    module.Cache.GetType(typeof(System.Console)), "WriteLine",
                    method => method.Parameters.Count == 5 &&
                    IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                    IntrinsicTypeSignature.Is(method.Parameters[1].ParameterType, IntrinsicType.Object) &&
                    IntrinsicTypeSignature.Is(method.Parameters[2].ParameterType, IntrinsicType.Object) &&
                    IntrinsicTypeSignature.Is(method.Parameters[3].ParameterType, IntrinsicType.Object) &&
                    IntrinsicTypeSignature.Is(method.Parameters[4].ParameterType, IntrinsicType.Object));

                this.writeLineFormatArray = module.FindMethod(
                    module.Cache.GetType(typeof(System.Console)), "WriteLine",
                    method => method.Parameters.Count == 2 &&
                    IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                    method.Parameters[1].ParameterType.BelongsToClassification(TypeClassifications.Array));
            }
        public void Initialize(ModuleDeclaration module)
        {
            this.module = module;
            this.loggingImplementation = new LoggingImplementationTypeBuilder(module);

            ITypeSignature traceTypeSignature = module.Cache.GetType(typeof(System.Diagnostics.Trace));

            this.writeLineString = module.FindMethod(traceTypeSignature, "WriteLine",
                method => method.Parameters.Count == 1 &&
                          IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String));

            this.traceInfoString = module.FindMethod(traceTypeSignature, "TraceInformation",
                method => method.Parameters.Count == 1 &&
                          IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String));

            this.traceInfoFormat = module.FindMethod(traceTypeSignature, "TraceInformation",
                method => method.Parameters.Count == 2 &&
                          IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                          method.Parameters[1].ParameterType.BelongsToClassification(TypeClassifications.Array));

            this.traceWarningString = module.FindMethod(traceTypeSignature, "TraceWarning",
                method => method.Parameters.Count == 1 &&
                          IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String));

            this.traceWarningFormat = module.FindMethod(traceTypeSignature, "TraceWarning",
                method => method.Parameters.Count == 2 &&
                          IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                          method.Parameters[1].ParameterType.BelongsToClassification(TypeClassifications.Array));

            this.traceErrorString = module.FindMethod(traceTypeSignature, "TraceError",
                method => method.Parameters.Count == 1 &&
                IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String));

            this.traceErrorFormat = module.FindMethod(traceTypeSignature, "TraceError",
                method => method.Parameters.Count == 2 &&
                          IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                          method.Parameters[1].ParameterType.BelongsToClassification(TypeClassifications.Array));
        }
        public void Initialize(ModuleDeclaration module)
        {
            this.module       = module;
            this.formatWriter = new StringFormatWriter(module);

            ITypeSignature traceTypeSignature = module.Cache.GetType(typeof(System.Diagnostics.Trace));

            this.writeLineString = module.FindMethod(traceTypeSignature, "WriteLine",
                                                     method => method.Parameters.Count == 1 &&
                                                     IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String));

            this.traceInfoString = module.FindMethod(traceTypeSignature, "TraceInformation",
                                                     method => method.Parameters.Count == 1 &&
                                                     IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String));

            this.traceInfoFormat = module.FindMethod(traceTypeSignature, "TraceInformation",
                                                     method => method.Parameters.Count == 2 &&
                                                     IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                                                     method.Parameters[1].ParameterType.BelongsToClassification(TypeClassifications.Array));

            this.traceWarningString = module.FindMethod(traceTypeSignature, "TraceWarning",
                                                        method => method.Parameters.Count == 1 &&
                                                        IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String));

            this.traceWarningFormat = module.FindMethod(traceTypeSignature, "TraceWarning",
                                                        method => method.Parameters.Count == 2 &&
                                                        IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                                                        method.Parameters[1].ParameterType.BelongsToClassification(TypeClassifications.Array));

            this.traceErrorString = module.FindMethod(traceTypeSignature, "TraceError",
                                                      method => method.Parameters.Count == 1 &&
                                                      IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String));

            this.traceErrorFormat = module.FindMethod(traceTypeSignature, "TraceError",
                                                      method => method.Parameters.Count == 2 &&
                                                      IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                                                      method.Parameters[1].ParameterType.BelongsToClassification(TypeClassifications.Array));
        }
 public Assets(ModuleDeclaration module)
 {
     this.ToStringMethodSignature = module.FindMethod(module.Cache.GetType(typeof(object)), "ToString");
 }
Beispiel #21
0
            public override void Implement(TransformationContext context)
            {
                ModuleDeclaration module = this.AspectWeaver.Module;

                TypeDefDeclaration typeDef = (TypeDefDeclaration)context.TargetElement;

                // Declare the method.
                MethodDefDeclaration methodDef = new MethodDefDeclaration
                {
                    Name              = "AutoGeneratedValidate",
                    Attributes        = (MethodAttributes.Family | MethodAttributes.ReuseSlot | MethodAttributes.Virtual),
                    CallingConvention = CallingConvention.HasThis
                };

                typeDef.Methods.Add(methodDef);
                methodDef.CustomAttributes.Add(this.AspectWeaver.AspectInfrastructureTask.WeavingHelper.GetDebuggerNonUserCodeAttribute());

                // Define parameter.
                methodDef.ReturnParameter = new ParameterDeclaration
                {
                    ParameterType = module.Cache.GetIntrinsic(IntrinsicType.Void),
                    Attributes    = ParameterAttributes.Retval
                };

                // Define the body
                MethodBodyDeclaration methodBody = new MethodBodyDeclaration();

                methodDef.MethodBody = methodBody;
                InstructionBlock instructionBlock = methodBody.CreateInstructionBlock();

                methodBody.RootInstructionBlock = instructionBlock;
                InstructionSequence sequence = methodBody.CreateInstructionSequence();

                instructionBlock.AddInstructionSequence(sequence, NodePosition.After, null);

                using (InstructionWriter writer = new InstructionWriter())
                {
                    writer.AttachInstructionSequence(sequence);

                    // Find the base method.
                    IMethod         baseValidateMethod = null;
                    IType           baseTypeCursor     = typeDef.BaseType;
                    MethodSignature methodSignature    =
                        new MethodSignature(module, CallingConvention.HasThis, module.Cache.GetIntrinsic(IntrinsicType.Void),
                                            new ITypeSignature[0], 0);

                    while (baseValidateMethod == null)
                    {
                        TypeDefDeclaration baseTypeCursorTypeDef = baseTypeCursor.GetTypeDefinition();

                        baseValidateMethod =
                            baseTypeCursorTypeDef.Methods.GetMethod("AutoGeneratedValidate",
                                                                    methodSignature.Translate(baseTypeCursorTypeDef.Module),
                                                                    BindingOptions.OnlyExisting |
                                                                    BindingOptions.DontThrowException);

                        baseTypeCursor = baseTypeCursorTypeDef.BaseType;
                    }

                    // TODO: support generic base types.

                    // Call the base method.
                    writer.EmitInstruction(OpCodeNumber.Ldarg_0);
                    writer.EmitInstructionMethod(OpCodeNumber.Call, (IMethod)baseValidateMethod.Translate(typeDef.Module));

                    // Make an array with the boxed field values.
                    TypeValidationAspect aspect = (TypeValidationAspect)this.AspectWeaverInstance.Aspect;
                    LocalVariableSymbol  fieldValuesArrayLocal = instructionBlock.DefineLocalVariable(
                        module.Cache.GetType(typeof(object[])), "fieldValues");
                    writer.EmitInstructionInt32(OpCodeNumber.Ldc_I4, aspect.Validators.Count);
                    writer.EmitInstructionType(OpCodeNumber.Newarr, module.Cache.GetIntrinsic(IntrinsicType.Object));
                    writer.EmitInstructionLocalVariable(OpCodeNumber.Stloc, fieldValuesArrayLocal);

                    int i = 0;
                    foreach (FieldValidationAttribute validator in aspect.Validators)
                    {
                        FieldDefDeclaration fieldDef = typeDef.Fields.GetByName(validator.TargetLocation.Name);
                        IField fieldSpec             = GenericHelper.GetFieldCanonicalGenericInstance(fieldDef);

                        writer.EmitInstructionLocalVariable(OpCodeNumber.Ldloc, fieldValuesArrayLocal);
                        writer.EmitInstructionInt32(OpCodeNumber.Ldc_I4, i);
                        writer.EmitInstruction(OpCodeNumber.Ldarg_0);
                        writer.EmitInstructionField(OpCodeNumber.Ldfld, fieldSpec);
                        writer.EmitConvertToObject(fieldSpec.FieldType);
                        writer.EmitInstruction(OpCodeNumber.Stelem_Ref);

                        i++;
                    }

                    // Get the validator method.
                    IMethod validateMethod = module.Cache.GetItem(
                        () => module.FindMethod(
                            typeof(TypeValidationAspect).GetMethod("Validate"),
                            BindingOptions.Default));

                    // Call the validator.
                    this.AspectWeaverInstance.AspectRuntimeInstanceField.EmitLoadField(writer, null);
                    writer.EmitInstructionLocalVariable(OpCodeNumber.Ldloc, fieldValuesArrayLocal);
                    writer.EmitInstructionMethod(OpCodeNumber.Callvirt, validateMethod);

                    writer.EmitInstruction(OpCodeNumber.Ret);
                    writer.DetachInstructionSequence();
                }
            }
Beispiel #22
0
 public Assets(ModuleDeclaration module)
 {
     OnThrow   = module.FindMethod(typeof(OnThrowAspectAttribute).GetMethod("OnThrow", new[] { typeof(Exception) }), BindingOptions.Default);
     Exception = module.FindType(typeof(Exception));
 }
 public Assets(ModuleDeclaration module)
 {
     OnThrow = module.FindMethod(typeof(OnThrowAspectAttribute).GetMethod("OnThrow", new[] { typeof(Exception) }), BindingOptions.Default);
     Exception = module.FindType(typeof(Exception));
 }
Beispiel #24
0
        public void Initialize(ModuleDeclaration module)
        {
            this.loggingImplementation = new LoggingImplementationTypeBuilder(module);
            this.formatWriter = new StringFormatWriter(module);

            this.loggerType = module.FindType(typeof(Logger));

            Predicate<MethodDefDeclaration> singleMessagePredicate =
                method => method.Parameters.Count == 1 &&
                    IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String);

            this.categoryInitializerMethod = module.FindMethod(module.FindType(typeof(LogManager)), "GetLogger", singleMessagePredicate);

            this.writeDebugMethod = module.FindMethod(this.loggerType, "Trace", singleMessagePredicate);
            this.writeInfoMethod = module.FindMethod(this.loggerType, "Info", singleMessagePredicate);
            this.writeWarnMethod = module.FindMethod(this.loggerType, "Warn", singleMessagePredicate);
            this.writeErrorMethod = module.FindMethod(this.loggerType, "Error", singleMessagePredicate);
            this.writeFatalMethod = module.FindMethod(this.loggerType, "Fatal", singleMessagePredicate);

            this.getIsTraceEnabledMethod = module.FindMethod(this.loggerType, "get_IsTraceEnabled");
            this.getIsInfoEnabledMethod = module.FindMethod(this.loggerType, "get_IsInfoEnabled");
            this.getIsWarnEnabledMethod = module.FindMethod(this.loggerType, "get_IsWarnEnabled");
            this.getIsErrorEnabledMethod = module.FindMethod(this.loggerType, "get_IsErrorEnabled");
            this.getIsFatalEnabledMethod = module.FindMethod(this.loggerType, "get_IsFatalEnabled");
        }
 public Assets(ModuleDeclaration module)
 {
     this.ToStringMethodSignature = module.FindMethod(module.Cache.GetType(typeof(object)), "ToString");
 }
Beispiel #26
0
        public void Initialize(ModuleDeclaration module)
        {
            this.loggingImplementation = new LoggingImplementationTypeBuilder(module);
            this.formatWriter          = new StringFormatWriter(module);

            this.loggerType = module.FindType(typeof(Logger));

            Predicate <MethodDefDeclaration> singleMessagePredicate =
                method => method.Parameters.Count == 1 &&
                IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String);

            this.categoryInitializerMethod = module.FindMethod(module.FindType(typeof(LogManager)), "GetLogger", singleMessagePredicate);

            this.writeDebugMethod = module.FindMethod(this.loggerType, "Trace", singleMessagePredicate);
            this.writeInfoMethod  = module.FindMethod(this.loggerType, "Info", singleMessagePredicate);
            this.writeWarnMethod  = module.FindMethod(this.loggerType, "Warn", singleMessagePredicate);
            this.writeErrorMethod = module.FindMethod(this.loggerType, "Error", singleMessagePredicate);
            this.writeFatalMethod = module.FindMethod(this.loggerType, "Fatal", singleMessagePredicate);

            this.getIsTraceEnabledMethod = module.FindMethod(this.loggerType, "get_IsTraceEnabled");
            this.getIsInfoEnabledMethod  = module.FindMethod(this.loggerType, "get_IsInfoEnabled");
            this.getIsWarnEnabledMethod  = module.FindMethod(this.loggerType, "get_IsWarnEnabled");
            this.getIsErrorEnabledMethod = module.FindMethod(this.loggerType, "get_IsErrorEnabled");
            this.getIsFatalEnabledMethod = module.FindMethod(this.loggerType, "get_IsFatalEnabled");
        }