nana1482
Messages postés1Date d'inscriptionlundi 12 septembre 2005StatutMembreDernière intervention18 avril 2007
-
18 avril 2007 à 18:57
cs_Bidou
Messages postés5487Date d'inscriptiondimanche 4 août 2002StatutMembreDernière intervention20 juin 2013
-
18 avril 2007 à 20:02
coucou,
voila j\'ai un petit souci. Je dois faire un projet pour mon école et débute seulement le langage c# et je dois faire de la reflexion. J\'ai trouver un code sur msn qui fait normalement ce que je souhaite oui mais voila je n\'arrive pas acceder au méthode get/set a partir d\'une autre classe(il reconnait la classe mais pas les methodes).
Si quelqu\'un pouvait m\'aider.
Merci
Le code pour creer ma dll :
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;
namespace ReflectionEmit
{
class ClassTestReflectionE
{
// The last argument of DefineProperty is null, because the
// property has no parameters. (If you don\'t specify null, you must
// specify an array of Type objects. For a parameterless property,
// use an array with no elements: new Type[] {})
PropertyBuilder custNamePropBldr = myTypeBuilder.DefineProperty("CustomerName",
PropertyAttributes.HasDefault,
typeof(string),
null);
// The property set and property get methods require a special
// set of attributes.
MethodAttributes getSetAttr =
MethodAttributes.Public | MethodAttributes.SpecialName |
MethodAttributes.HideBySig;
// Define the "get" accessor method for CustomerName.
MethodBuilder custNameGetPropMthdBldr =
myTypeBuilder.DefineMethod("get_CustomerName",
getSetAttr,
typeof(string),
Type.EmptyTypes);
// Last, we must map the two methods created above to our PropertyBuilder to
// their corresponding behaviors, "get" and "set" respectively.
custNamePropBldr.SetGetMethod(custNameGetPropMthdBldr);
custNamePropBldr.SetSetMethod(custNameSetPropMthdBldr);
Type retval = myTypeBuilder.CreateType();
// Save the assembly so it can be examined with Ildasm.exe,
// or referenced by a test program.
myAsmBuilder.Save(myAsmName.Name + ".dll");
cs_Bidou
Messages postés5487Date d'inscriptiondimanche 4 août 2002StatutMembreDernière intervention20 juin 201361 18 avril 2007 à 19:19
Salut,
C'est un peu compliqué si tu veux juste accéder aux méthodes ou aux properties.
Voici quelques chose de plus simple:
// Le path vers l'assembly
string path = "myAssemblyPath";
Assembly a = Assembly.LoadFile(path);
// Un type contenu dans l'assembly
Type myType = a.GetType(myType);
FieldInfo myProp = myType.GetProperty(myProperty);
// recherche d'une property
MethodInfo myMeth = myType.GetMethod((myMethod); // recherche d'une méthode
// Par exemple, on peut invoquer la méthode
myMeth.Invoke(myType, myParameters);
cs_Bidou
Messages postés5487Date d'inscriptiondimanche 4 août 2002StatutMembreDernière intervention20 juin 201361 18 avril 2007 à 20:02
Ces temps je comprends la moitié des questions de travers
(j'ai cru qu'il fallait accéder à une assembly par reflexion, pas en créer une dynamiquement).
Rien de mieux comme sujet pour débutant que System.Reflexion.Emit pour commencer à programmer en C#