public InterfaceProxyWithOptionalTargetContributor(INamingScope namingScope, ModuleScope scope, GetTargetExpressionDelegate getTarget, GetTargetReferenceDelegate getTargetReference) : base(namingScope, scope, getTarget) { this.getTargetReference = getTargetReference; canChangeTarget = true; }
public void ModuleScopeCanHandleSignedAndUnsignedInParallel() { var scope = new ModuleScope(); Assert.IsNull(scope.StrongNamedModule); Assert.IsNull(scope.WeakNamedModule); var one = scope.ObtainDynamicModuleWithStrongName(); Assert.IsNotNull(scope.StrongNamedModule); Assert.IsNull(scope.WeakNamedModule); Assert.AreSame(one, scope.StrongNamedModule); var two = scope.ObtainDynamicModuleWithWeakName(); Assert.IsNotNull(scope.StrongNamedModule); Assert.IsNotNull(scope.WeakNamedModule); Assert.AreSame(two, scope.WeakNamedModule); Assert.AreNotSame(one, two); Assert.AreNotSame(one.Assembly, two.Assembly); var three = scope.ObtainDynamicModuleWithStrongName(); var four = scope.ObtainDynamicModuleWithWeakName(); Assert.AreSame(one, three); Assert.AreSame(two, four); }
public ClassProxyWithTargetTargetContributor(Type targetType, IList<MethodInfo> methodsToSkip, INamingScope namingScope, ModuleScope scope) : base(namingScope, scope) { this.targetType = targetType; this.methodsToSkip = methodsToSkip; }
public void ModuleScopeStoresModuleBuilder() { var scope = new ModuleScope(); var one = scope.ObtainDynamicModuleWithStrongName(); var two = scope.ObtainDynamicModuleWithStrongName(); Assert.AreSame(one, two); Assert.AreSame(one.Assembly, two.Assembly); }
public ClassProxyWithTargetGenerator(ModuleScope scope, Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options) : base(scope, classToProxy) { CheckNotGenericTypeDefinition(targetType, "targetType"); EnsureDoesNotImplementIProxyTargetAccessor(targetType, "targetType"); CheckNotGenericTypeDefinitions(additionalInterfacesToProxy, "additionalInterfacesToProxy"); options.Initialize(); ProxyGenerationOptions = options; this.additionalInterfacesToProxy = TypeUtil.GetAllInterfaces(additionalInterfacesToProxy).ToArray(); }
public void ImplicitModulePaths() { var scope = new ModuleScope(true); Assert.AreEqual(ModuleScope.DEFAULT_FILE_NAME, scope.StrongNamedModuleName); Assert.AreEqual(Path.Combine(Directory.GetCurrentDirectory(), ModuleScope.DEFAULT_FILE_NAME), scope.ObtainDynamicModuleWithStrongName().FullyQualifiedName); Assert.IsNull(scope.StrongNamedModuleDirectory); Assert.AreEqual(ModuleScope.DEFAULT_FILE_NAME, scope.WeakNamedModuleName); Assert.AreEqual(Path.Combine(Directory.GetCurrentDirectory(), ModuleScope.DEFAULT_FILE_NAME), scope.ObtainDynamicModuleWithWeakName().FullyQualifiedName); Assert.IsNull(scope.WeakNamedModuleDirectory); }
public ClassEmitter(ModuleScope modulescope, String name, Type baseType, Type[] interfaces, TypeAttributes flags, bool forceUnsigned) : base(CreateTypeBuilder (modulescope, name, baseType, interfaces, flags, forceUnsigned)) { InitializeGenericArgumentsFromBases(ref baseType, ref interfaces); if (interfaces != null) { foreach(Type inter in interfaces) { TypeBuilder.AddInterfaceImplementation(inter); } } TypeBuilder.SetParent(baseType); }
public void ExplicitModulePaths () { ModuleScope scope = new ModuleScope (true, "Strong", "StrongModule.dll", "Weak", "WeakModule.dll"); Assert.AreEqual ("StrongModule.dll", scope.StrongNamedModuleName); Assert.AreEqual (Path.Combine (Environment.CurrentDirectory, "StrongModule.dll"), scope.ObtainDynamicModuleWithStrongName ().FullyQualifiedName); Assert.IsNull (scope.StrongNamedModuleDirectory); Assert.AreEqual ("WeakModule.dll", scope.WeakNamedModuleName); Assert.AreEqual (Path.Combine (Environment.CurrentDirectory, "WeakModule.dll"), scope.ObtainDynamicModuleWithWeakName ().FullyQualifiedName); Assert.IsNull (scope.WeakNamedModuleDirectory); scope = new ModuleScope (true, "Strong", @"c:\Foo\StrongModule.dll", "Weak", @"d:\Bar\WeakModule.dll"); Assert.AreEqual ("StrongModule.dll", scope.StrongNamedModuleName); Assert.AreEqual (@"c:\Foo\StrongModule.dll", scope.ObtainDynamicModuleWithStrongName ().FullyQualifiedName); Assert.AreEqual (@"c:\Foo", scope.StrongNamedModuleDirectory); Assert.AreEqual ("WeakModule.dll", scope.WeakNamedModuleName); Assert.AreEqual (@"d:\Bar\WeakModule.dll", scope.ObtainDynamicModuleWithWeakName ().FullyQualifiedName); Assert.AreEqual (@"d:\Bar", scope.WeakNamedModuleDirectory); }
protected CompositeTypeContributor(INamingScope namingScope, ModuleScope scope) { this.scope = scope; this.namingScope = namingScope; }
public CustomClassEmitter(ModuleScope scope, string name, Type baseType) : this(scope, name, baseType, Type.EmptyTypes, TypeAttributes.Class | TypeAttributes.Public, false) { }
public ClassProxyGenerator(ModuleScope scope, Type targetType) : base(scope, targetType) { CheckNotGenericTypeDefinition(targetType, "targetType"); EnsureDoesNotImplementIProxyTargetAccessor(targetType, "targetType"); }
public void ModuleScopeDoesntTryToDeleteFromCurrentDirectory() { var moduleDirectory = Path.Combine(Directory.GetCurrentDirectory(), "GeneratedDlls"); if (Directory.Exists(moduleDirectory)) Directory.Delete(moduleDirectory, true); var strongModulePath = Path.Combine(moduleDirectory, "Strong.dll"); var weakModulePath = Path.Combine(moduleDirectory, "Weak.dll"); Directory.CreateDirectory(moduleDirectory); var scope = new ModuleScope(true, false, "Strong", strongModulePath, "Weak", weakModulePath); using (File.Create(Path.Combine(Directory.GetCurrentDirectory(), "Strong.dll"))) { scope.ObtainDynamicModuleWithStrongName(); scope.SaveAssembly(true); // this will throw if SaveAssembly tries to delete from the current directory } using (File.Create(Path.Combine(Directory.GetCurrentDirectory(), "Weak.dll"))) { scope.ObtainDynamicModuleWithWeakName(); scope.SaveAssembly(false); // this will throw if SaveAssembly tries to delete from the current directory } // Clean up the generated DLLs because the FileStreams are now closed Directory.Delete(moduleDirectory, true); File.Delete(Path.Combine(Directory.GetCurrentDirectory(), "Strong.dll")); File.Delete(Path.Combine(Directory.GetCurrentDirectory(), "Weak.dll")); }
public ClassProxyWithTargetGenerator(ModuleScope scope, Type targetType, Type[] interfaces, ProxyGenerationOptions options) : base(scope, targetType, interfaces, options) { }
public void SaveWithFlagFalseDoesntThrowsWhenMultipleAssembliesGenerated() { var scope = new ModuleScope(false); scope.ObtainDynamicModuleWithStrongName(); scope.ObtainDynamicModuleWithWeakName(); scope.SaveAssembly(); }
public ExtensibleClassProxyGenerator(ModuleScope scope, string proxyNamespace, Type targetType, params ITypeContributor[] contributors) : base(scope, targetType) { this.contributors = contributors; this.proxyNamespace = proxyNamespace; }
public StableBindingProxyProvider(ITypeFilter typeFilter, ModuleScope moduleScope) { _typeFilter = typeFilter; _moduleScope = moduleScope; }
private void SaveAndVerifyModuleScopeAssembly(ModuleScope moduleScope, bool strongNamed) { var path = moduleScope.SaveAssembly(strongNamed); PEVerifier.CreateDefault().VerifyPEFile(path); }
protected BaseClassProxyGenerator(ModuleScope scope, Type targetType, Type[] interfaces, ProxyGenerationOptions options) : base(scope, targetType, interfaces, options) { EnsureDoesNotImplementIProxyTargetAccessor(targetType, nameof(targetType)); }
public ComputedServiceProxyGeneratorImpl(ModuleScope scope, Type targetType) : base(scope, targetType) { }
public DelegateProxyGenerator(ModuleScope scope, Type delegateType) : base(scope, delegateType) { ProxyGenerationOptions = new ProxyGenerationOptions(new DelegateProxyGenerationHook()); ProxyGenerationOptions.Initialize(); }
public DocumentProxyGenerator(ModuleScope scope, Type targetType) : base(scope, targetType) { }
public void SaveUnsigned() { var scope = new ModuleScope(true); scope.ObtainDynamicModuleWithWeakName(); var path = ModuleScope.DEFAULT_FILE_NAME; if (File.Exists(path)) File.Delete(path); Assert.IsFalse(File.Exists(path)); var savedPath = scope.SaveAssembly(); Assert.AreEqual(savedPath, Path.GetFullPath(path)); CheckUnsignedSavedAssembly(path); File.Delete(path); }
/// <summary> /// Saves the generated assembly to a physical file. Note that this renders the <see cref = "PersistentProxyBuilder" /> unusable. /// </summary> /// <returns>The path of the generated assembly file, or null if no assembly has been generated.</returns> /// <remarks> /// This method does not support saving multiple files. If both a signed and an unsigned module have been generated, use the /// respective methods of the <see cref = "ModuleScope" />. /// </remarks> public string SaveAssembly() { return(ModuleScope.SaveAssembly()); }
public void SaveReturnsNullWhenNoModuleObtained() { var scope = new ModuleScope(true); Assert.IsNull(scope.SaveAssembly()); }
public ClassEmitter(ModuleScope modulescope, String name, Type baseType, IEnumerable <Type> interfaces) : this(modulescope, name, baseType, interfaces, DefaultAttributes, ShouldForceUnsigned()) { }
public void ExplicitSaveThrowsWhenSpecifiedAssemblyNotGeneratedStrongName() { var scope = new ModuleScope(true); scope.ObtainDynamicModuleWithWeakName(); Assert.Throws<InvalidOperationException>(() => scope.SaveAssembly(true)); }
public EasyType(ModuleScope modulescope, String name, Type baseType, Type[] interfaces) : this(modulescope, name, baseType, interfaces, false) { }
public void GeneratedAssembliesDefaultName() { var scope = new ModuleScope(); var strong = scope.ObtainDynamicModuleWithStrongName(); var weak = scope.ObtainDynamicModuleWithWeakName(); Assert.AreEqual(ModuleScope.DEFAULT_ASSEMBLY_NAME, strong.Assembly.GetName().Name); Assert.AreEqual(ModuleScope.DEFAULT_ASSEMBLY_NAME, weak.Assembly.GetName().Name); }
public EasyType(ModuleScope modulescope, String name) : this(modulescope, name, typeof(object), new Type[0]) { }
public void LoadAssemblyIntoCache_InvalidAssembly() { var newScope = new ModuleScope(false); Assert.Throws<ArgumentException>(() => newScope.LoadAssemblyIntoCache(Assembly.GetExecutingAssembly()) ); }
public void LoadAssemblyIntoCache_InvalidAssembly() { var newScope = new ModuleScope(false); newScope.LoadAssemblyIntoCache(Assembly.GetExecutingAssembly()); }
public InterfaceProxyWithoutTargetContributor(INamingScope namingScope, ModuleScope scope, GetTargetExpressionDelegate getTarget) : base(namingScope, scope) { getTargetExpression = getTarget; }
private static TypeBuilder CreateTypeBuilder(ModuleScope modulescope, string name, Type baseType, Type[] interfaces, TypeAttributes flags, bool forceUnsigned) { bool isAssemblySigned = !forceUnsigned && !StrongNameUtil.IsAnyTypeFromUnsignedAssembly(baseType, interfaces); return modulescope.ObtainDynamicModule(isAssemblySigned).DefineType(name, flags); }
public void Init() { module = new ModuleScope(); }
public InterfaceProxyWithTargetInterfaceTargetContributor(Type proxyTargetType, bool allowChangeTarget, INamingScope namingScope, ModuleScope scope) : base(proxyTargetType, allowChangeTarget, namingScope, scope) { }
public WrappedClassMembersCollector(Type type, ModuleScope scope) : base(type, scope) { }
protected BaseProxyGenerator(ModuleScope scope, Type targetType) { this.scope = scope; this.targetType = targetType; }
public Implementation(ModuleScope scope, Type @interface, Options options) : base(scope, @interface) => Options = options;
public void SaveWithPath() { var strongModulePath = Path.GetTempFileName(); var weakModulePath = Path.GetTempFileName(); File.Delete(strongModulePath); File.Delete(weakModulePath); Assert.IsFalse(File.Exists(strongModulePath)); Assert.IsFalse(File.Exists(weakModulePath)); var scope = new ModuleScope(true,false, "Strong", strongModulePath, "Weak", weakModulePath); scope.ObtainDynamicModuleWithStrongName(); scope.ObtainDynamicModuleWithWeakName(); scope.SaveAssembly(true); scope.SaveAssembly(false); Assert.IsTrue(File.Exists(strongModulePath)); Assert.IsTrue(File.Exists(weakModulePath)); File.Delete(strongModulePath); File.Delete(weakModulePath); }
public InterfaceProxyWithTargetInterfaceGenerator(ModuleScope scope, Type @interface) : base(scope, @interface) { }
public void SaveThrowsWhenMultipleAssembliesGenerated() { var scope = new ModuleScope(true); scope.ObtainDynamicModuleWithStrongName(); scope.ObtainDynamicModuleWithWeakName(); Assert.Throws<InvalidOperationException>(() => scope.SaveAssembly()); }
public void ExplicitSaveWorksEvenWhenMultipleAssembliesGenerated() { var scope = new ModuleScope(true); scope.ObtainDynamicModuleWithStrongName(); scope.ObtainDynamicModuleWithWeakName(); scope.SaveAssembly(true); CheckSignedSavedAssembly(ModuleScope.DEFAULT_FILE_NAME); scope.SaveAssembly(false); CheckUnsignedSavedAssembly(ModuleScope.DEFAULT_FILE_NAME); File.Delete(ModuleScope.DEFAULT_FILE_NAME); }
public void LoadAssemblyIntoCache_DifferentGenerationOptions() { var savedScope = new ModuleScope(true); var builder = new DefaultProxyBuilder(savedScope); var options1 = new ProxyGenerationOptions(); options1.AddMixinInstance(new DateTime()); var options2 = ProxyGenerationOptions.Default; var cp1 = builder.CreateClassProxyType(typeof (object), Type.EmptyTypes, options1); var cp2 = builder.CreateClassProxyType(typeof (object), Type.EmptyTypes, options2); Assert.AreNotSame(cp1, cp2); Assert.AreSame(cp1, builder.CreateClassProxyType(typeof (object), Type.EmptyTypes, options1)); Assert.AreSame(cp2, builder.CreateClassProxyType(typeof (object), Type.EmptyTypes, options2)); var path = savedScope.SaveAssembly(); CrossAppDomainCaller.RunInOtherAppDomain(delegate(object[] args) { var newScope = new ModuleScope(false); var newBuilder = new DefaultProxyBuilder(newScope); var assembly = Assembly.LoadFrom((string) args[0]); newScope.LoadAssemblyIntoCache(assembly); var newOptions1 = new ProxyGenerationOptions(); newOptions1.AddMixinInstance(new DateTime()); var newOptions2 = ProxyGenerationOptions.Default; var loadedCP1 = newBuilder.CreateClassProxyType(typeof (object), Type.EmptyTypes, newOptions1); var loadedCP2 = newBuilder.CreateClassProxyType(typeof (object), Type.EmptyTypes, newOptions2); Assert.AreNotSame(loadedCP1, loadedCP2); Assert.AreEqual(assembly, loadedCP1.Assembly); Assert.AreEqual(assembly, loadedCP2.Assembly); }, path); File.Delete(path); }
public void SavedAssemblyHasCacheMappings() { var scope = new ModuleScope(true); scope.ObtainDynamicModuleWithWeakName(); var savedPath = scope.SaveAssembly(); CrossAppDomainCaller.RunInOtherAppDomain(delegate(object[] args) { var assembly = Assembly.LoadFrom((string) args[0]); Assert.IsTrue(assembly.IsDefined(typeof (CacheMappingsAttribute), false)); }, savedPath); File.Delete(savedPath); }
public static void ResetScope() { _scope = new ModuleScope(); }
public void CacheMappingsHoldTypes() { var scope = new ModuleScope(true); var builder = new DefaultProxyBuilder(scope); var cp = builder.CreateClassProxyType(typeof (object), Type.EmptyTypes, ProxyGenerationOptions.Default); var savedPath = scope.SaveAssembly(); CrossAppDomainCaller.RunInOtherAppDomain(delegate(object[] args) { var assembly = Assembly.LoadFrom((string) args[0]); var attribute = (CacheMappingsAttribute) assembly.GetCustomAttributes(typeof (CacheMappingsAttribute), false)[0]; var entries = attribute.GetDeserializedMappings(); Assert.AreEqual(1, entries.Count); var key = new CacheKey(typeof (object), new Type[0], ProxyGenerationOptions.Default); Assert.IsTrue(entries.ContainsKey(key)); Assert.AreEqual(args[1], entries[key]); }, savedPath, cp.FullName); File.Delete(savedPath); }
public void GeneratedAssembliesWithCustomName() { var scope = new ModuleScope(false,false, "Strong", "Module1.dll", "Weak", "Module2,dll"); var strong = scope.ObtainDynamicModuleWithStrongName(); var weak = scope.ObtainDynamicModuleWithWeakName(); Assert.AreEqual("Strong", strong.Assembly.GetName().Name); Assert.AreEqual("Weak", weak.Assembly.GetName().Name); }
public void GenerateDynamicAssemblyHavingModopts() { // One test below tries to proxy a type that has a method whose signature // involves an optional modifier (modopt). These are never produced by the // C# nor VB.NET compilers, but the C++/CLI compiler produces those. However, // if we added a C++/CLI project to this solution, it could only be compiled // on Windows (since the only extant C++/CLI compiler is MSVC). So what we do // instead to get some modopts is to generate a dynamic test assembly. // // Instead of using System.Reflection.AssemblyBuilder directly, we use // Castle's own ModuleScope since that seems the easiest way to get a strong- // named persistent assembly that can be referenced by DynamicProxy. const string assemblyName = "Rhino.Mocks.CPP.Interfaces"; const string assemblyFileName = "Rhino.Mocks.CPP.Interfaces.dll"; var moduleScope = new ModuleScope( savePhysicalAssembly: true, disableSignedModule: false, namingScope: new Generators.NamingScope(), strongAssemblyName: assemblyName, strongModulePath: assemblyFileName, weakAssemblyName: assemblyName, weakModulePath: assemblyFileName); // This is the C++/CLI type we want to generate: // // namespace RhinoMocksCPPInterfaces { // public interface class IHaveMethodWithModOpts { // virtual void StartLiveOnSlot(long int slotNumber); // }; // } // // which corresponds to this IL: // // .class interface public abstract auto ansi beforefieldinit RhinoMocksCPPInterfaces.IHaveMethodWithModOpts // { // .method public hidebysig newslot abstract virtual instance void StartLiveOnSlot(int32 modopt([mscorlib]System.Runtime.CompilerServices.IsLong) slotNumber) cil managed // { } // } var typeBuilder = moduleScope.DefineType( true, "RhinoMocksCPPInterfaces.IHaveMethodWithModOpts", TypeAttributes.Class | TypeAttributes.Interface | TypeAttributes.Public | TypeAttributes.Abstract | TypeAttributes.AutoLayout | TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit); var methodBuilder = typeBuilder.DefineMethod( "StartLiveOnSlot", MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Abstract | MethodAttributes.Virtual, returnType: typeof(void), returnTypeRequiredCustomModifiers: null, returnTypeOptionalCustomModifiers: null, parameterTypes: new[] { typeof(int) }, parameterTypeRequiredCustomModifiers: null, parameterTypeOptionalCustomModifiers: new[] { new[] { typeof(IsLong) } }, callingConvention: CallingConventions.Standard); var iHaveMethodWithModOptsType = typeBuilder.CreateTypeInfo(); #if FEATURE_ASSEMBLYBUILDER_SAVE if (IsVerificationPossible) { // Let's persist and PE-verify the dynamic assembly before it gets used in tests: var assemblyPath = moduleScope.SaveAssembly(); RunPEVerifyOnGeneratedAssembly(assemblyPath); } #endif this.iHaveMethodWithModOptsType = iHaveMethodWithModOptsType; }
public void DefaultProxyBuilderWithSpecificScope() { var scope = new ModuleScope(false); var builder = new DefaultProxyBuilder(scope); Assert.AreSame(scope, builder.ModuleScope); }
/// <summary> /// Initializes a new instance of the <see cref="ServiceHostInterfaceProxyGenerator"/> class. /// </summary> /// <param name="scope">The scope of the module being built.</param> /// <param name="interface">The interface that will be proxied.</param> public ServiceHostInterfaceProxyGenerator(ModuleScope scope, Type @interface) : base(scope, @interface) { _metadataBuddyType = @interface.GetMetadataClassType(); }
static EntityHelper() { var scope = new ModuleScope(false, false, new EntityProxyNameScope(), ASSEMBLY_NAME, MODULE_PATH, ASSEMBLY_NAME, MODULE_PATH); generator = new ProxyGenerator(new DefaultProxyBuilder(scope)); }
protected MembersCollector(Type type, ModuleScope scope) { this.scope = scope; this.type = type; }
private void CheckLoadAssemblyIntoCache(ProxyCreator creator) { var savedScope = new ModuleScope(true); var builder = new DefaultProxyBuilder(savedScope); var cp = creator(builder); Assert.AreSame(cp, creator(builder)); var path = savedScope.SaveAssembly(); CrossAppDomainCaller.RunInOtherAppDomain(delegate(object[] args) { var newScope = new ModuleScope(false); var newBuilder = new DefaultProxyBuilder(newScope); var assembly = Assembly.LoadFrom((string) args[0]); newScope.LoadAssemblyIntoCache(assembly); var loadedCP = assembly.GetType((string) args[1]); Assert.AreSame(loadedCP, ((ProxyCreator) args[2])(newBuilder)); Assert.AreEqual(assembly, ((ProxyCreator) args[2])(newBuilder).Assembly); }, path, cp.FullName, creator); File.Delete(path); }
public InterfaceProxyWithTargetGenerator(ModuleScope scope, Type @interface) : base(scope, @interface) { CheckNotGenericTypeDefinition(@interface, "@interface"); }
public InterfaceProxyTargetContributor(Type proxyTargetType, bool canChangeTarget, INamingScope namingScope, ModuleScope scope) : base(namingScope, scope) { this.proxyTargetType = proxyTargetType; this.canChangeTarget = canChangeTarget; }
public ClassEmitter(ModuleScope modulescope, String name, Type baseType, IEnumerable <Type> interfaces, TypeAttributes flags) : this(modulescope, name, baseType, interfaces, flags, forceUnsigned : false) { }