示例#1
0
        private DynamicType(Type interfaceType, OrderedDictionary<Type, List<MethodInfo, MethodInfo>> methods, List<Delegate, MethodInfo> delegates, List<MethodInfo> stubs) {
            var counter = 0;
 
            _dynamicType = DefineDynamicType(interfaceType);
 
            // if the declaring type contains an OUE method declaration, then the host is permitting the plugin to see their oewn unhandled exceptions for debugging purposes.
            OnUnhandledException = interfaceType.GetVirtualMethods().FirstOrDefault(each => each.Name == "OnUnhandledException" && each.GetParameterTypes().SequenceEqual(new Type[] {typeof (string), typeof (Exception)}));

            foreach (var instanceType in methods.Keys) {
                // generate storage for object
                var field = _dynamicType.DefineField("_instance_{0}".format(++counter), instanceType, FieldAttributes.Private);
                _storageFields.Add(field);

                // create methods

                foreach (var method in methods[instanceType]) {
                    _dynamicType.GenerateMethodForDirectCall(method.Key, field, method.Value, OnUnhandledException);
                    _implementedMethods.Add(method.Key.Name);
                }
            }

            foreach (var d in delegates) {
                var field = _dynamicType.DefineField("_delegate_{0}".format(++counter), d.Key.GetType(), FieldAttributes.Private);
                _storageFields.Add(field);
                _implementedMethods.Add(d.Value.Name);

                _dynamicType.GenerateMethodForDelegateCall(d.Value, field, OnUnhandledException);
            }

            foreach (var method in stubs) {
                // did not find a matching method or signature, or the instance told us that it doesn't actually support it
                // that's ok, if we get here, it must not be a required method.
                // we'll implement a placeholder method for it.
                _dynamicType.GenerateStubMethod(method);
            }

            _dynamicType.GenerateIsMethodImplemented();

            // generate the constructor for the class.
            DefineConstructor(interfaceType.GetTypeInfo().IsInterface ? typeof (Object) : interfaceType);
        }