2009-12-25 8 views
14

Je veux apprendre la méthode dynamique et son exemple pratique en utilisant C#.
Existe-t-il une relation entre la méthode dynamique et la réflexion?
S'il vous plaît aidez-moi.Exemple pratique de méthode dynamique?

+0

Delphi a des méthodes dynamiques, pour lesquelles C# n'a pas d'analogie, et il y a une technique de programmation appelée _dynamic programming_. Ou voulez-vous dire _virtual method_? Je suppose, par "Reflation", que vous voulez dire la réflexion. –

+3

aucun homme il n'y a aussi un concept de méthode dynamique dans C# – Pankaj

Répondre

5

Vous pouvez créer une méthode via la classe DynamicMethod.

DynamicMethod squareIt = new DynamicMethod(
    "SquareIt", 
    typeof(long), 
    methodArgs, 
    typeof(Example).Module); 

ILGenerator il = squareIt.GetILGenerator(); 
il.Emit(OpCodes.Ldarg_0); 
il.Emit(OpCodes.Conv_I8); 
il.Emit(OpCodes.Dup); 
il.Emit(OpCodes.Mul); 
il.Emit(OpCodes.Ret); 

Fully commented example on MSDN
Je dois souligner que le développement de cette méthode est très lent et pas très utile.

+0

@ VMAtm .. j'avais lu un article de DynamicMethod, quelque part j'ai trouvé qu'ils sont plus rapides que réflexion.DynamicMethod aussi ils éliminent le besoin d'écrire le code personnalisé de sérialisation et de désérialisation. – Pankaj

+1

Je veux dire la vitesse de développement - il n'est pas très rapide et très difficile de maintenir un tel code. – VMAtm

+0

ILGenerator - fait partie de System.Reflection.Emit Dans un peu c'est une relation entre la réflexion et les méthodes dynamiques – VMAtm

8

Nous utilisons des méthodes dynamiques pour accélérer la réflexion. Voici le code de notre optimiseur de réflexion. il est seulement 10% plus lent que l'appel direct et 2000 fois plus rapide que l'appel de réflexion

public class ReflectionEmitPropertyAccessor 
    { 
     private readonly bool canRead; 
     private readonly bool canWrite; 
     private IPropertyAccessor emittedPropertyAccessor; 
     private readonly string propertyName; 
     private readonly Type propertyType; 
     private readonly Type targetType; 
     private Dictionary<Type,OpCode> typeOpCodes; 

     public ReflectionEmitPropertyAccessor(Type targetType, string property) 
     { 
      this.targetType = targetType; 
      propertyName = property; 
      var propertyInfo = targetType.GetProperty(property); 
      if (propertyInfo == null) 
      { 
       throw new ReflectionOptimizerException(string.Format("Property \"{0}\" is not found in type "+ "{1}.", property, targetType)); 
      } 
      canRead = propertyInfo.CanRead; 
      canWrite = propertyInfo.CanWrite; 
      propertyType = propertyInfo.PropertyType; 
     } 

     public bool CanRead 
     { 
      get { return canRead; } 
     } 

     public bool CanWrite 
     { 
      get { return canWrite; } 
     } 

     public Type TargetType 
     { 
      get { return targetType; } 
     } 

     public Type PropertyType 
     { 
      get { return propertyType; } 
     } 

     #region IPropertyAccessor Members 

     public object Get(object target) 
     { 
      if (canRead) 
      { 
       if (emittedPropertyAccessor == null) 
       { 
        Init(); 
       } 

       if (emittedPropertyAccessor != null) return emittedPropertyAccessor.Get(target); 

      } 
      else 
      { 
       throw new ReflectionOptimizerException(string.Format("У свойства \"{0}\" нет метода get.", propertyName)); 
      } 
      throw new ReflectionOptimizerException("Fail initialize of " + GetType().FullName); 
     } 

     public void Set(object target, object value) 
     { 
      if (canWrite) 
      { 
       if (emittedPropertyAccessor == null) 
       { 
        Init(); 
       } 
       if (emittedPropertyAccessor != null) emittedPropertyAccessor.Set(target, value); 
      } 
      else 
      { 
       throw new ReflectionOptimizerException(string.Format("Property \"{0}\" does not have method set.", propertyName)); 
      } 
      throw new ReflectionOptimizerException("Fail initialize of " + GetType().FullName); 
     } 

     #endregion 

     private void Init() 
     { 
      InitTypeOpCodes(); 
      var assembly = EmitAssembly(); 
      emittedPropertyAccessor = assembly.CreateInstance("Property") as IPropertyAccessor; 
      if (emittedPropertyAccessor == null) 
      { 
       throw new ReflectionOptimizerException("Shit happense in PropertyAccessor."); 
      } 
     } 

     private void InitTypeOpCodes() 
     { 
      typeOpCodes = new Dictionary<Type, OpCode> 
          { 
           {typeof (sbyte), OpCodes.Ldind_I1}, 
           {typeof (byte), OpCodes.Ldind_U1}, 
           {typeof (char), OpCodes.Ldind_U2}, 
           {typeof (short), OpCodes.Ldind_I2}, 
           {typeof (ushort), OpCodes.Ldind_U2}, 
           {typeof (int), OpCodes.Ldind_I4}, 
           {typeof (uint), OpCodes.Ldind_U4}, 
           {typeof (long), OpCodes.Ldind_I8}, 
           {typeof (ulong), OpCodes.Ldind_I8}, 
           {typeof (bool), OpCodes.Ldind_I1}, 
           {typeof (double), OpCodes.Ldind_R8}, 
           {typeof (float), OpCodes.Ldind_R4} 
          }; 
     } 

     private Assembly EmitAssembly() 
     { 
      var assemblyName = new AssemblyName {Name = "PropertyAccessorAssembly"}; 
      var newAssembly = Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run); 
      var newModule = newAssembly.DefineDynamicModule("Module"); 
      var dynamicType = newModule.DefineType("Property", TypeAttributes.Public); 
      dynamicType.AddInterfaceImplementation(typeof(IPropertyAccessor)); 
      dynamicType.DefineDefaultConstructor(MethodAttributes.Public); 
      var getParamTypes = new[] { typeof(object) }; 
      var getReturnType = typeof(object); 
      var getMethod = dynamicType.DefineMethod("Get", 
            MethodAttributes.Public | MethodAttributes.Virtual, 
            getReturnType, 
            getParamTypes); 

      var getIL = getMethod.GetILGenerator(); 
      var targetGetMethod = targetType.GetMethod("get_" + propertyName); 

      if (targetGetMethod != null) 
      { 
       getIL.DeclareLocal(typeof(object)); 
       getIL.Emit(OpCodes.Ldarg_1); //Load the first argument 
       getIL.Emit(OpCodes.Castclass, targetType); //Cast to the source type 
       getIL.EmitCall(OpCodes.Call, targetGetMethod, null); //Get the property value 
       if (targetGetMethod.ReturnType.IsValueType) 
       { 
        getIL.Emit(OpCodes.Box, targetGetMethod.ReturnType); //Box 
       } 
       getIL.Emit(OpCodes.Stloc_0); //Store it 
       getIL.Emit(OpCodes.Ldloc_0); 
      } 
      else 
      { 
       getIL.ThrowException(typeof(MissingMethodException)); 
      } 

      getIL.Emit(OpCodes.Ret); 
      var setParamTypes = new[] { typeof(object), typeof(object) }; 
      const Type setReturnType = null; 
      var setMethod = dynamicType.DefineMethod("Set", 
            MethodAttributes.Public | MethodAttributes.Virtual, 
            setReturnType, 
            setParamTypes); 

      var setIL = setMethod.GetILGenerator(); 

      var targetSetMethod = targetType.GetMethod("set_" + propertyName); 
      if (targetSetMethod != null) 
      { 
       Type paramType = targetSetMethod.GetParameters()[0].ParameterType; 

       setIL.DeclareLocal(paramType); 
       setIL.Emit(OpCodes.Ldarg_1); //Load the first argument //(target object) 
       setIL.Emit(OpCodes.Castclass, targetType); //Cast to the source type 
       setIL.Emit(OpCodes.Ldarg_2); //Load the second argument 
       //(value object) 
       if (paramType.IsValueType) 
       { 
        setIL.Emit(OpCodes.Unbox, paramType); //Unbox it  
        if (typeOpCodes.ContainsKey(paramType)) //and load 
        { 
         var load = typeOpCodes[paramType]; 
         setIL.Emit(load); 
        } 
        else 
        { 
         setIL.Emit(OpCodes.Ldobj, paramType); 
        } 
       } 
       else 
       { 
        setIL.Emit(OpCodes.Castclass, paramType); //Cast class 
       } 
       setIL.EmitCall(OpCodes.Callvirt,targetSetMethod, null); //Set the property value 
      } 
      else 
      { 
       setIL.ThrowException(typeof(MissingMethodException)); 
      } 
      setIL.Emit(OpCodes.Ret); 
      // Load the type 
      dynamicType.CreateType(); 
      return newAssembly; 
     } 

    } 

mise en œuvre sont agrégées à partir de différentes sources et principale est cet article CodeProject. Je ne comprends pas le terme _dynamic method_

+0

Merci @@ Sergey Miroda ... onw plus de chose Y at-il une relation entre la méthode dynamique et la réflexion – Pankaj

+0

Oui c'est. Reflection.Emit fait partie de la réflexion .net. –

+2

aussi la méthode dynamique est la méthode __only__ si vous voulez au code de construction d'exécution qui peut être garbage collecté. –

Questions connexes