ERLEDIGT
NEIN
NEIN
ANTWORTEN
3
3
ZUGRIFFE
3382
3382
EMPFEHLEN
-
11.07.06 20:19 #1Hallo!
Hier mal ein kleines Beispiel wie man unter .Net Dynamic Proxies in C# erzeugen kann:
Code csharp:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
using System; using System.Collections.Generic; using System.Text; using RealProxy = System.Runtime.Remoting.Proxies.RealProxy; using IMessage = System.Runtime.Remoting.Messaging.IMessage; using ReturnMessage = System.Runtime.Remoting.Messaging.ReturnMessage; using IMethodCallMessage = System.Runtime.Remoting.Messaging.IMethodCallMessage; using TargetInvocationException = System.Reflection.TargetInvocationException; namespace De.Tutorials.Training { class DynamicProxyExample { public static void Main(string[] args) { IBusinessService businessService = new SimpleProxy(typeof(IBusinessService), new BusinessServiceImpl()).GetTransparentProxy() as IBusinessService; Console.WriteLine(businessService.BusinessOperation("Proxy")); } } class SimpleProxy : RealProxy { object target; public SimpleProxy(Type type, object target) : base(type) { this.target = target; } public override IMessage Invoke(IMessage message) { IMethodCallMessage methodCallMessage = (IMethodCallMessage)message; try { Console.WriteLine("Before: " + methodCallMessage.MethodName); object result = target.GetType().GetMethod(methodCallMessage.MethodName, (Type[])methodCallMessage.MethodSignature).Invoke(target, methodCallMessage.InArgs); Console.WriteLine("After: " + methodCallMessage.MethodName); return new ReturnMessage(result, null, 0, methodCallMessage.LogicalCallContext, methodCallMessage); } catch (TargetInvocationException targetInvocationException) { return new ReturnMessage(targetInvocationException, methodCallMessage); } } } interface IBusinessService { string BusinessOperation(string args); } class BusinessServiceImpl : IBusinessService { public string BusinessOperation(string args) { Console.WriteLine("Hallo " + args); return args; } } }
Ausgabe:
Code :1 2 3 4
Before: BusinessOperation Hallo Proxy After: BusinessOperation Proxy
Gruß TomJava rocks! http://www.jugsaar.de
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
-
09.07.08 15:28 #2
Mitglied Brokat
- Registriert seit
- May 2007
- Ort
- Riedstadt (Hessen)
- Beiträge
- 354
Hallo Thomas,
unter Java kann ich die Proxies schachteln. Wenn ich das hier mache, fliegt eine Exception(Object does not match target type). Gibt es dafür eine Lösung?
Grüße
LimagoI didn't write this; a very complex macro did.
-
09.07.08 15:55 #3
Hallo,
das kann man auch unter .Net ohne Probleme
Hier ein Beispiel mit Transparent Proxies:
Code csharp:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
using System; using System.Collections.Generic; using System.Diagnostics; using IMethodCallMessage = System.Runtime.Remoting.Messaging.IMethodCallMessage; using RealProxy = System.Runtime.Remoting.Proxies.RealProxy; using ReturnMessage = System.Runtime.Remoting.Messaging.ReturnMessage; namespace De.Tutorials.Training { public class DynamicProxyExample { public static void Main(string[] args) { IDataModel<string> simpleDataModel = (IDataModel<string>)new DataModelProxy(typeof(IDataModel<string>)).GetTransparentProxy(); simpleDataModel.Data = "Bubu"; Console.WriteLine(simpleDataModel is IDataModel<string>); Console.WriteLine(simpleDataModel.Data); Console.WriteLine(simpleDataModel.GetType().FullName); IDataModel<IDataModel<string>> wrappedDataModel = (IDataModel<IDataModel<string>>)new DataModelProxy(typeof(IDataModel<IDataModel<string>>)).GetTransparentProxy(); wrappedDataModel.Data = simpleDataModel; Console.WriteLine("####"); Console.WriteLine(wrappedDataModel is IDataModel<IDataModel<string>>); Console.WriteLine(wrappedDataModel.Data == null); Console.WriteLine(wrappedDataModel.Data.Data); Console.WriteLine(wrappedDataModel.GetType().FullName); } public class DataModelProxy : RealProxy { IDictionary<string, object> propertyDictionary; public DataModelProxy(Type type) : base(type) { this.propertyDictionary = new Dictionary<string, object>(); } public override System.Runtime.Remoting.Messaging.IMessage Invoke(System.Runtime.Remoting.Messaging.IMessage aMessage) { IMethodCallMessage methodInvocationMessage = (IMethodCallMessage)aMessage; if (methodInvocationMessage.MethodName.Equals("GetType")) { return new ReturnMessage(this.GetType(), null, 0, methodInvocationMessage.LogicalCallContext, methodInvocationMessage); } if (methodInvocationMessage.MethodName.StartsWith("set_")) { string propertyName = GetPropertyNameFor(methodInvocationMessage.MethodName); this.propertyDictionary[propertyName] = methodInvocationMessage.Args[0]; return new ReturnMessage(methodInvocationMessage.Args[0], null, 0, methodInvocationMessage.LogicalCallContext, methodInvocationMessage); } else if (methodInvocationMessage.MethodName.StartsWith("get_")) { string propertyName = GetPropertyNameFor(methodInvocationMessage.MethodName); if (this.propertyDictionary.ContainsKey(propertyName)) { return new ReturnMessage(this.propertyDictionary[propertyName], null, 0, methodInvocationMessage.LogicalCallContext, methodInvocationMessage); } else { return new ReturnMessage(null, null, 0, methodInvocationMessage.LogicalCallContext, methodInvocationMessage); } } else { return new ReturnMessage(null, null, 0, methodInvocationMessage.LogicalCallContext, methodInvocationMessage); } } private string GetPropertyNameFor(string propertyName) { return propertyName.Substring(4); } } public interface IDataModel<TData> { TData Data { get; set; } } } }
Witzig, dass man nach und nach die Java Jünger im .Net Teich plantschen sieht...
Aber wie gesagt, es ist wichtig beides zu kennen!
Es gibt natürlich zahlreiche andere Möglichkeiten Dynamic Proxies zu erzeugen. Transparent Proxies sind ziemlich schwer zu debuggen, da man oft die Meldung bekommt, dass das Objekt nicht ausgewertet werden kann, da es sich um einen Remote Proxy handelt... hab die genaue Meldung nicht mehr im Kopf.
Eine Alternative zu den ätzenden transpoarent Remoting Proxies sind beispielsweise Castles Dynamic Proxies, Springs Dynamic Proxies oder eigene Proxies mit System.Reflection.Emit (machen wir in der Firma auch selbst... und die kann man sogar ordentlich Debuggen
)
Schau mal hier:
http://www.tutorials.de/forum/net-ap...c-proxies.html
http://www.tutorials.de/forum/net-ap...pring-net.html
Gruß TomJava rocks! http://www.jugsaar.de
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
-
10.07.08 17:10 #4
Mitglied Brokat
- Registriert seit
- May 2007
- Ort
- Riedstadt (Hessen)
- Beiträge
- 354
Ja, ja so ist es wohl

Schön Dich auch hier hier zu finden und vielen Dank für Deine prompte Antwort. Die Spring Proxies hatte ich auch schon gefunden, aber ich brauche eine Lösung mit "Bordmitteln".
Die TransparentProxies sind in der Tat nicht so schön, aber für mein Problem völlig ausreichend.
Viele Grüße
LimagoI didn't write this; a very complex macro did.
Ähnliche Themen
-
Dynamic Web Project unter Eclipse
Von messmar im Forum Enterprise Java (JEE, J2EE, Spring & Co.)Antworten: 1Letzter Beitrag: 12.06.10, 23:55 -
Dynamic Proxy in Python
Von Thomas Darimont im Forum CGI, Perl, Python, Ruby, Power ShellAntworten: 0Letzter Beitrag: 14.05.07, 13:16 -
Proxy unter Debian
Von zerix im Forum NetzwerkeAntworten: 5Letzter Beitrag: 30.09.05, 19:20 -
Proxy unter Suse Linux 7.2
Von phantom im Forum Linux & UnixAntworten: 0Letzter Beitrag: 03.03.02, 16:37 -
Proxy-Server-Einrichtung unter SuSe Linux 7.3
Von Arne Buchwald im Forum Linux & UnixAntworten: 5Letzter Beitrag: 30.01.02, 22:37





Zitieren
Login