public void Test_MapTo_CastleProxy() { Castle.DynamicProxy.ProxyGenerator proxyGenerator = new Castle.DynamicProxy.ProxyGenerator(); var proxy = proxyGenerator.CreateClassProxy <DtoSample>(); proxy.Name = "a"; EntitySample sample = proxy.MapTo <EntitySample>(); Assert.Equal("a", sample.Name); var proxy2 = proxyGenerator.CreateClassProxy <DtoSample>(); proxy2.Name = "b"; sample = proxy2.MapTo <EntitySample>(); Assert.Equal("b", sample.Name); }
public void Castle() { var gen = new Castle.DynamicProxy.ProxyGenerator(); var proxy = gen.CreateClassProxy <ItExtendsAnAbstractClass>(new CastleInterceptor()); Assert.Equal(-3, proxy.Return(string.Empty)); }
public void WriteDynamicProxyObjectTest() { var list = new List <TestClass>(); var proxyGenerator = new Castle.DynamicProxy.ProxyGenerator(); for (var i = 0; i < 1; i++) { var proxy = proxyGenerator.CreateClassProxy <TestClass>(); proxy.Id = i + 1; proxy.Name = "name" + proxy.Id; list.Add(proxy); } using (var stream = new MemoryStream()) using (var reader = new StreamReader(stream)) using (var writer = new StreamWriter(stream)) using (var csv = new CsvWriter(writer)) { csv.Configuration.Delimiter = ","; csv.Configuration.RegisterClassMap <TestClassMap>(); csv.WriteRecords(list); writer.Flush(); stream.Position = 0; var data = reader.ReadToEnd(); var expected = new StringBuilder(); expected.AppendLine("id,name"); expected.AppendLine("1,name1"); Assert.AreEqual(expected.ToString(), data); } }
protected SerializationHelperFirst(SerializationInfo info, StreamingContext context) { _info = info; _context = context; Castle.DynamicProxy.ProxyGenerator generator = new Castle.DynamicProxy.ProxyGenerator(); _proxy = (SimpleHardcodedPlusHelperProxy) generator.CreateClassProxy(typeof(SimpleHardcodedPlusHelperProxy), new Castle.Core.Interceptor.IInterceptor[0]); }
public void SerializeDoubleProxy() { Castle.DynamicProxy.ProxyGenerator generator = new Castle.DynamicProxy.ProxyGenerator(); SimpleHardcodedPlusHelperProxy proxy = (SimpleHardcodedPlusHelperProxy) generator.CreateClassProxy(typeof(SimpleHardcodedPlusHelperProxy), new Castle.Core.Interceptor.IInterceptor[0]); proxy.Name = "test"; SimpleHardcodedPlusHelperProxy deserialized = (SimpleHardcodedPlusHelperProxy)Cloner.GetSerializedClone(proxy); Assert.AreEqual(deserialized.Name, "test"); }
public void ProxiesAndHostsWCF() { #if DEBUG // On release mode, castle is ILMerged into Moq.dll and this won't compile var generator = new Castle.DynamicProxy.ProxyGenerator(); var proxy = generator.CreateClassProxy <ServiceImplementation>(); using (var host = new WebServiceHost(proxy, new Uri("http://localhost:7777"))) { host.Open(); } #endif }
protected override object VisitCreateInstance(CreateInstanceCallSite createInstanceCallSite, ServiceProviderEngineScope scope) { try { object obj; var interceptors = InterceptorRuntimeCreate.CreatedInterceptors; var implementName = createInstanceCallSite.ImplementationType.FullName; if (interceptors != null && interceptors.Count > 0 && InterceptorRuntimeCreate.CanIntercept(implementName)) { Castle.DynamicProxy.ProxyGenerator generator = new Castle.DynamicProxy.ProxyGenerator(); if (createInstanceCallSite.ServiceType.IsInterface) { obj = generator.CreateClassProxy(createInstanceCallSite.ImplementationType, new[] { createInstanceCallSite.ServiceType }, interceptors.ToArray()); } else { obj = generator.CreateClassProxy(createInstanceCallSite.ImplementationType, interceptors.ToArray()); } } else { obj = Activator.CreateInstance(createInstanceCallSite.ImplementationType); } //return Activator.CreateInstance(createInstanceCallSite.ImplementationType); return(obj); } catch (Exception ex) when(ex.InnerException != null) { ExceptionDispatchInfo.Capture(ex.InnerException).Throw(); // The above line will always throw, but the compiler requires we throw explicitly. throw; } }
/// <summary> /// 创建一个类型方式的拦截器(可传入参数) /// </summary> /// <param name="classType"></param> /// <param name="arguments"></param> /// <param name="interceptors"></param> /// <returns></returns> public static object CreateProxy(Type classType, object[] arguments, params Castle.DynamicProxy.IInterceptor[] interceptors) { //如果拦截器为0 if (interceptors.Length == 0) { return(Activator.CreateInstance(classType, arguments)); } Castle.DynamicProxy.ProxyGenerator proxy = new Castle.DynamicProxy.ProxyGenerator(); Castle.DynamicProxy.ProxyGenerationOptions options = new Castle.DynamicProxy.ProxyGenerationOptions(new ProxyGenerationHook()) { Selector = new InterceptorSelector() }; if (arguments == null || arguments.Length == 0) { return(proxy.CreateClassProxy(classType, options, interceptors)); } else { return(proxy.CreateClassProxy(classType, options, arguments, interceptors)); } }
public void Test_MapTo_CastleProxy() { var proxyGenerator = new Castle.DynamicProxy.ProxyGenerator(); var proxy = proxyGenerator.CreateClassProxy <DtoSample>(); proxy.Name = "a"; var sample = proxy.MapTo <EntitySample>(); Assert.Equal("a", sample.Name); var proxy2 = proxyGenerator.CreateClassProxy <DtoSample>(); proxy2.Name = "b"; sample = proxy2.MapTo <EntitySample>(); Assert.Equal("b", sample.Name); var sample2 = new DtoSample { Name = "c" }; var proxy3 = proxyGenerator.CreateClassProxy <EntitySample>(); sample2.MapTo(proxy3); Assert.Equal("c", proxy3.Name); }
/// <summary> /// Creates an instance of proxy class. /// </summary> /// <param name="targetType">The type from which the proxy class is inherited.</param> /// <returns>The proxy class instance.</returns> public object CreateClassProxy(Type targetType) { Check.NotNull(targetType, nameof(targetType)); if (targetType.IsValueType) { throw new ProxyGeneratorException(string.Format("Value type {0} is not supported for proxy generation.", targetType.Name)); } if (targetType == typeof(string)) { throw new ProxyGeneratorException(string.Format("Type String is not supported for proxy generation.")); } if (IsTypeAccessible(targetType)) { throw new TypeAccessiblityException(string.Format("Type {0} is not accessible.", targetType.FullName)); } if (targetType.IsInterface) { return(generator.CreateInterfaceProxyWithoutTarget(targetType)); } var defaultCtor = GetContructorsForType(targetType).FirstOrDefault(x => x.GetParameters().Count() == 0); if (IsConstructorAccessible(defaultCtor)) { try { return(generator.CreateClassProxy(targetType)); } catch (Castle.DynamicProxy.ProxyGenerationException x) { throw new TypeAccessiblityException("Castle instance creation error", x); } } return(UseAnyAvailableConstructor(targetType)); }
public static object CreateProxy(SitecoreClassConfig config, ISitecoreService service, Item item, bool inferType) { object proxy = null; Type type = config.Type; if (type.IsInterface) { proxy = _generator.CreateInterfaceProxyWithoutTarget(type, new InterfaceMethodInterceptor(config, item, service)); } else { proxy = _generator.CreateClassProxy(type, _options, new ProxyClassInterceptor(type, service, item, inferType)); } return(proxy); }
public void DoubleProxyTest() { Type generatedType = DynamicPropertyChangedProxy.CreateBusinessObjectProxy( typeof(SimpleBusinessObject), new Type[0]); Castle.DynamicProxy.ProxyGenerator generator = new Castle.DynamicProxy.ProxyGenerator(); SimpleBusinessObject proxy = (SimpleBusinessObject) generator.CreateClassProxy(generatedType, new Castle.Core.Interceptor.IInterceptor[0]); }
static void Main(string[] args) { var proxyGenerator = new Castle.DynamicProxy.ProxyGenerator(); var entity = proxyGenerator.CreateClassProxy(typeof(Article), new object[0], new TestInterceptor()) as Article;; var json = JsonConvert.SerializeObject(entity); }
public void SerializationCastleDynamicProxy2() { Castle.DynamicProxy.ProxyGenerator generator = new Castle.DynamicProxy.ProxyGenerator(); SimpleBusinessObject proxy = (SimpleBusinessObject) generator.CreateClassProxy(typeof(SimpleBusinessObject), new Castle.Core.Interceptor.IInterceptor[0]); MemoryStream stream = new MemoryStream(); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, proxy); stream.Position = 0; object deserializedObject = formatter.Deserialize(stream); Assert.IsNotNull(deserializedObject); }
public void UndoChangesSGO_CastleDynProxy() { Castle.DynamicProxy.ProxyGenerator generator = new Castle.DynamicProxy.ProxyGenerator(); SimpleGenericObject<string> proxy = (SimpleGenericObject<string>) generator.CreateClassProxy(typeof(SimpleGenericObject<string>), new Castle.Core.Interceptor.IInterceptor[0]); proxy.GenericArray = new string[] { "test1", "test2" }; proxy.GenericArray[0] = "test3"; proxy.Code = "code1"; Assert.AreEqual(proxy.Code, "code1"); Assert.AreEqual(proxy.GenericArray[0], "test3"); }
public void CastleDynamicProxy2Test() { Castle.DynamicProxy.ProxyGenerator generator = new Castle.DynamicProxy.ProxyGenerator(); SimpleExplicitPropChanged proxy = (SimpleExplicitPropChanged) generator.CreateClassProxy(typeof(SimpleExplicitPropChanged), new Castle.Core.Interceptor.IInterceptor[0]); }
/// <summary> /// 创建一个类型方式的拦截器(可传入参数) /// </summary> /// <param name="classType"></param> /// <param name="arguments"></param> /// <param name="interceptors"></param> /// <returns></returns> public static object CreateProxy(Type classType, object[] arguments, params Castle.DynamicProxy.IInterceptor[] interceptors) { //如果拦截器为0 if (interceptors.Length == 0) { return Activator.CreateInstance(classType, arguments); } Castle.DynamicProxy.ProxyGenerator proxy = new Castle.DynamicProxy.ProxyGenerator(); Castle.DynamicProxy.ProxyGenerationOptions options = new Castle.DynamicProxy.ProxyGenerationOptions(new ProxyGenerationHook()) { Selector = new InterceptorSelector() }; if (arguments == null || arguments.Length == 0) return proxy.CreateClassProxy(classType, options, interceptors); else return proxy.CreateClassProxy(classType, options, arguments, interceptors); }