Zurück tutorials.de > Home

 
 
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 :-)

Themen: 242.975 | Beiträge: 1.352.293 | Mitglieder: 169.418 (Stand 28.01.10) | Fragen zur Nutzung von Tutorials.de? Nutzungsregeln | Kontaktformular | Impressum

Jubiläums-Countdown 23.02 23.03 23.04 23.05 23.06 23.07 23.08 23.09


4 kostenlose Bücher bei unserer Buch-Verschenkaktion 03/2010
Einzelnen Beitrag anzeigen
 
Alt 09.07.08, 15:55   #3 (permalink)
 
Benutzerbild von Thomas Darimont tutorials.de Administrator 
 
Registriert seit: Jun 2002
Ort: Saarbrücken (Saarland)
Beiträge: 9.165
Renommee-Modifikator: 61
Thomas Darimont hat eine strahlende ZukunftThomas Darimont hat eine strahlende ZukunftThomas Darimont hat eine strahlende ZukunftThomas Darimont hat eine strahlende ZukunftThomas Darimont hat eine strahlende ZukunftThomas Darimont hat eine strahlende ZukunftThomas Darimont hat eine strahlende ZukunftThomas Darimont hat eine strahlende Zukunft

AW: Dynamic Proxy unter .Net

Hallo,

das kann man auch unter .Net ohne Probleme Hier ein Beispiel mit Transparent Proxies:
csharp Code:
  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ß Tom
__________________
Java rocks!
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
  Thomas Darimont ist offline  
 
» Tools
 
tutorials.de-Tools tutorial.de-Suchfeld tutorial.de-Widget tutorial.de-RSS-Feed tutorial.de-Banner
» Neue Links
 
Hits: 138
»
JHT's Planetary...
(Cinema 4D-Objekte)
Hits: 265
»
Tageslicht ohne GI
(Cinema 4D-Tutorials)
Hits: 154
»
Puzzle
(Cinema 4D-Tutorials)
Hits: 104
»
Lacreme
(Cinema 4D-Tutorials)
Hits: 195
»
Liquid Light
(Cinema 4D-Tutorials)
» Aktuelle Umfrage
 
Bist du mit der Geschwindigkeit der Tutorials.de-Website zufrieden?
Ja, es putzt mir glatt den Staub vom Bildschirm! - 80,61%
158 Stimmen
Nein, ich denke da muss noch nachgebessert werden... - 19,39%
38 Stimmen
Stimmen gesamt: 196
Du darfst bei dieser Umfrage nicht abstimmen.

 

Alle Zeitangaben in WEZ +1. Es ist jetzt 16:44 Uhr.


Powered by vBulletin® Version 3.8.5 (Deutsch) & vBadvanced CMPS v.3.2.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Alle Rechte vorbehalten ©2000 - 2010 tutorials.de
Design by Mark, CSS by Maik & Sven Mintel
Seite generiert in 0,31465 Sekunden mit 24 queries