|
|
| |
| |
Hallo und herzlich willkommen! Tutorials.de ist eine Hilfe-Community mit dem Motto User helfen Usern. Als Gast verfügst Du über Schreibrechte in unseren Foren und Blogs. Du kannst dich aber gerne auch kostenlos registrieren und Teil unserer Gemeinschaft werden! Viel Spaß & Erfolg bei der Vermehrung deines Wissens :-)
|
|
|
|
|
|
|
|
|
11.07.06, 20:19
|
#1 (permalink)
|
Registriert seit: Jun 2002
Ort: Saarbrücken (Saarland)
Beiträge: 9.155
|
Dynamic Proxy unter .Net
Hallo!
Hier mal ein kleines Beispiel wie man unter .Net Dynamic Proxies in C# erzeugen kann:
|
csharp Code:
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
| using System.Collections.Generic; 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 public SimpleProxy(Type type, object target) public override IMessage Invoke(IMessage message) IMethodCallMessage methodCallMessage = (IMethodCallMessage)message; 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 public string BusinessOperation(string args) Console.WriteLine("Hallo " + args);
|
|
Ausgabe:
|
Code:
|
Before: BusinessOperation
Hallo Proxy
After: BusinessOperation
Proxy
|
Gruß Tom
|
09.07.08, 15:28
|
#2 (permalink)
|
|
Mitglied Brokat
Registriert seit: May 2007
Ort: Riedstadt (Hessen)
Beiträge: 350
Renommee-Modifikator: 7
|
AW: Dynamic Proxy unter .Net
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
Limago
__________________
I didn't write this; a very complex macro did.
|
09.07.08, 15:55
|
#3 (permalink)
|
Registriert seit: Jun 2002
Ort: Saarbrücken (Saarland)
Beiträge: 9.155
|
AW: Dynamic Proxy unter .Net
Hallo,
das kann man auch unter .Net ohne Probleme  Hier ein Beispiel mit Transparent Proxies:
|
csharp Code:
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
| 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) 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 ); return new ReturnMessage (null, null, 0, methodInvocationMessage. LogicalCallContext, methodInvocationMessage ); return new ReturnMessage (null, null, 0, methodInvocationMessage. LogicalCallContext, methodInvocationMessage ); private string GetPropertyNameFor(string propertyName) return propertyName.Substring(4); public interface IDataModel<TData>
|
|
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ß Tom
|
10.07.08, 17:10
|
#4 (permalink)
|
|
Mitglied Brokat
Registriert seit: May 2007
Ort: Riedstadt (Hessen)
Beiträge: 350
Renommee-Modifikator: 7
|
AW: Dynamic Proxy unter .Net
Zitat:
Zitat von Thomas Darimont
Witzig, dass man nach und nach die Java Jünger im .Net Teich plantschen sieht... 
Aber wie gesagt, es ist wichtig beides zu kennen! 
|
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
Limago
__________________
I didn't write this; a very complex macro did.
|
|
| Themen-Optionen |
|
|
| Ansicht |
Linear-Darstellung
|
|
 |
|
»
Neue Tutorials
|
 |
|
|
|
|
|
|
|
|
|
|
|
|
»
Letzte News
|
 |
|
|
|
|
|
|
|
|
|
|
»
Tools
|
 |
|
|
|
|
|
»
Neue Links
|
 |
|
|
|
|
(Cinema 4D-Objekte)
|
|
(Cinema 4D-Tutorials)
|
|
(Cinema 4D-Tutorials)
|
|
(Cinema 4D-Tutorials)
|
|
(Cinema 4D-Tutorials)
|
|
»
Jobs @ tutorials.de
|
 |
|
|
|
|
|
|
|
|
|
|