private U Convert <T, U>(object value) { var cache = new ProxyTypeCache(); var proxyType = ProxyTypeEmitter.GetProxyType(cache, typeof(U), typeof(T)); Assert.NotNull(proxyType); var proxy = Activator.CreateInstance(proxyType, value); return(Assert.IsAssignableFrom <U>(proxy)); }
public void GetProxyType_Assignable(Type sourceType, Type targetType) { // Arrange var cache = new ProxyTypeCache(); // Act var result = ProxyTypeEmitter.GetProxyType(cache, targetType, sourceType); // Assert Assert.Null(result); }
private object ConvertTo(object value, Type type) { var cache = new ProxyTypeCache(); var proxyType = ProxyTypeEmitter.GetProxyType(cache, type, value.GetType()); Assert.NotNull(proxyType); var proxy = Activator.CreateInstance(proxyType, value); Assert.IsAssignableFrom(type, proxy); return(proxy); }
public void GetProxyType_Identity(Type type) { // Arrange var cache = new ProxyTypeCache(); // Act var result = ProxyTypeEmitter.GetProxyType(cache, type, type); // Assert Assert.Null(result); }
public void GetProxyType_LocksPreventDuplicateAssemblyNamesArgumentException_ForConcurrentThreads() { for (var i = 0; i < 5; i++) { Parallel.For( 0, 100, (j) => { var testObject = new Person(); ProxyTypeEmitter.GetProxyType(new ProxyTypeCache(), typeof(IPerson), testObject.GetType()); }); } }
public TProxy CreateProxy <TProxy>(object obj) { if (obj == null) { return(default(TProxy)); } else if (typeof(TProxy).GetTypeInfo().IsAssignableFrom(obj.GetType().GetTypeInfo())) { return((TProxy)obj); } var type = ProxyTypeEmitter.GetProxyType(_cache, typeof(TProxy), obj.GetType()); return((TProxy)Activator.CreateInstance(type, obj)); }
public void Adapt_InvalidProxy_IndexerProperty() { // Arrange var sourceType = typeof(Indexer); var targetType = typeof(IIndexer); var expected = $"The property 'Item' on type '{targetType}' must not define a setter to support proxy generation."; // Act & Assert var exception = Assert.Throws <InvalidProxyOperationException>( () => ProxyTypeEmitter.GetProxyType(new ProxyTypeCache(), targetType, sourceType)); Assert.Equal(expected, exception.Message); }
public void Adapt_Proxy_InvalidProperty_DestinationIsNotInterface() { // Arrange var sourceType = typeof(Person); var targetType = typeof(IBadPerson); var expectedMessage = string.Format( "Type '{0}' must be an interface in order to support proxy generation from source type '{1}'.", typeof(string), typeof(Address).FullName); // Act var exception = Assert.Throws <InvalidProxyOperationException>( () => ProxyTypeEmitter.GetProxyType(new ProxyTypeCache(), targetType, sourceType)); // Assert Assert.Equal(expectedMessage, exception.Message); }
public TProxy CreateProxy <TProxy>(object obj) { if (obj == null) { return(default(TProxy)); } else if (typeof(TProxy).GetTypeInfo().IsAssignableFrom(obj.GetType().GetTypeInfo())) { return((TProxy)obj); } #if NETCOREAPP2_0 || NET461 var type = ProxyTypeEmitter.GetProxyType(_cache, typeof(TProxy), obj.GetType()); return((TProxy)Activator.CreateInstance(type, obj)); #elif NETSTANDARD2_0 throw new PlatformNotSupportedException("This platform does not support creating proxy types and methods."); #else #error Target frameworks should be updated #endif }
public void GetProxyType_IfAlreadyInCache_AlsoAddedToVisited_FromError() { // Arrange var targetType = typeof(IPerson); var sourceType = typeof(Person); var key = new Tuple <Type, Type>(sourceType, targetType); var cache = new ProxyTypeCache(); cache[key] = ProxyTypeCacheResult.FromError(key, "Test Error"); var context = new ProxyTypeEmitter.ProxyBuilderContext(cache, targetType, sourceType); // Act var result = ProxyTypeEmitter.VerifyProxySupport(context, key); // Assert Assert.False(result); Assert.Equal(key, context.Visited.Single().Key); }
public void GetProxyType_IfAlreadyInCache_AlsoAddedToVisited_FromType() { // Arrange var targetType = typeof(IPerson); var sourceType = typeof(Person); var key = new Tuple <Type, Type>(sourceType, targetType); var cache = new ProxyTypeCache(); cache[key] = ProxyTypeCacheResult.FromType(key, sourceType, sourceType.GetConstructor(Array.Empty <Type>())); var context = new ProxyTypeEmitter.ProxyBuilderContext(cache, targetType, sourceType); // Act var result = ProxyTypeEmitter.VerifyProxySupport(context, key); var result2 = ProxyTypeEmitter.VerifyProxySupport(context, key); // Assert Assert.True(result); Assert.True(result2); Assert.Single(context.Visited); Assert.Equal(key, context.Visited.Single().Key); }