Felder ohne Reflection auslesen

  • C#

Es gibt 29 Antworten in diesem Thema. Der letzte Beitrag () ist von LaMiy.

    Immer noch "None".
    Ich muss mich aber korrigieren. Mit dem Code aus meinem vorherigen Post bekomme ich einen "Cannot cast from source type to destination type" error.
    Undzwar in Zeile 12. (Debug.Log, bei dir Zeile 11) Also ein Konvertierungsfehler. Der ist nicht viel weiter schlimm nehme ich an.

    Ich bin gerade noch am Debuggen. Aktualisiere den Post dann wahrscheinlich wenn ich noch eine Idee habe.
    also mMn ist Reflection genau dafür geschaffen damit sparst du dir dann bei einer Ausführung, bei welcher du diese Zugriffsweise nicht mehr brauchst Performance/Speicher...
    Und für Live-Editing ist Reflection aufjedenfall schnell genug
    Ich wollte auch mal ne total überflüssige Signatur:
    ---Leer---
    Du meinst auf den Kram verzichten und komplett Reflection benutztn?

    In meinem Anwendugsfall kann es sein, dass so ein Aufruf einer Variable jedes Frame (in einem Spiel) aufgerufen wird.
    Da hab ich doch ein bisschen Angst mit der Performance.

    Alle Properties auslesen und binden scheint mir performanter zu sein.


    So, ich hab nicht mehr viel rausfinden können. Im Dictionary lassen sich auch keine Hinweise finden.
    Schon beim GetValue() scheint der Wert None zu sein.
    Bilder
    • list.png

      63,24 kB, 1.459×331, 45 mal angesehen
    • none.png

      7,7 kB, 684×188, 45 mal angesehen
    Reflection nur verwenden für das klicki-bunti zeug für die Ausführung anschließend kannst du ja dann aus den Informationen Code erzeugen lassen das dürfte das performanteste sein ;)

    Edit: bei dem bisherigen Code liest du den Wert aus, willst du nicht viel mehr die getter-Funktion haben? Und diese verwenden?
    Ich wollte auch mal ne total überflüssige Signatur:
    ---Leer---

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „jvbsl“ ()

    dann würde ich auf GetGetMethod zurückgreifen:
    msdn.microsoft.com/de-de/library/e17dw503(v=vs.110).aspx
    Nen Delegaten kann man so erzeugen:
    msdn.microsoft.com/en-us/library/53cz7sc6.aspx

    bei neuerem Framework sieht auch das Interessant aus(hab ich aber noch nie benutzt):
    msdn.microsoft.com/en-us/libra…n.property(v=vs.110).aspx
    Ich wollte auch mal ne total überflüssige Signatur:
    ---Leer---
    @jvbsl
    Danke dir.

    Habe es zur Einfachheit halber mal außerhalb des Dictionaries probiert.

    C#-Quellcode

    1. public delegate object Get(Transform t);
    2. public void Init()
    3. {
    4. PropertyInfo[] infos = typeof(Transform).GetProperties(BindingFlags.Public | BindingFlags.Instance);
    5. foreach (PropertyInfo info in infos)
    6. {
    7. if (info.Name == "hideFalgs")
    8. continue;
    9. MethodInfo method = info.GetGetMethod();
    10. //Debug.Log(method.ReturnType);
    11. //Debug.Log(method.GetParameters().Length);
    12. var del = System.Delegate.CreateDelegate(typeof(Get), method);
    13. Get get;
    14. get = (Get)del;
    15. object obj = get(this.Value);
    16. Debug.Log(obj);


    Fehler fliegt in Zeile 17. "Method return type is incompatible".
    Sollte aber eigentlich nicht passieren, denn der Typ von position (erste PropertyInfo) ist Vector3, und der vom delegaten object.

    Ich habe das hier aus Beispiel2 entnommen msdn.microsoft.com/en-us/library/53cz7sc6.aspx.
    Die benutzen allerdings eine eigene Oberklasse.

    Vector3 ist in Unity struct. Ich weiß nicht ob das relevant ist.

    Habe auch var del = Get.CreateDelegate(typeof(Get), method); probiert. Selber Fehler.

    Aber eigtnlich sollte es doch klappen.
    Compatible Parameter Types and Return Type
    In the .NET Framework version 2.0, the parameter types and return type of a delegate created using this method overload must be compatible with the parameter types and return type of the method the delegate represents; the types do not have to match exactly. This represents a relaxation of the binding behavior in the .NET Framework version 1.0 and 1.1, where the types must match exactly.

    A parameter of a delegate is compatible with the corresponding parameter of a method if the type of the delegate parameter is more restrictive than the type of the method parameter, because this guarantees that an argument passed to the delegate can be passed safely to the method.

    Similarly, the return type of a delegate is compatible with the return type of a method if the return type of the method is more restrictive than the return type of the delegate, because this guarantees that the return value of the method can be cast safely to the return type of the delegate.

    For example, a delegate with a parameter of type Hashtable and a return type of Object can represent a method with a parameter of type Object and a return value of type Hashtable.


    Edit: Ändere ich den Rückgabetyp des Delegaten auf Vector3, dann funktioniert es für alle Properties vom Typ Vector3, bis dann ein anderer Typ kommt.
    Ich hab mal eine lauffähige Sample-Solution angehangen.
    Selber Fehler. (Heißt jetzt nur anders)

    Edit: Ja so wie das immer ist. Gerade die Solution hochgeladen und dann die Lösung gefunden.

    C#-Quellcode

    1. public delegate T1 Get<T1>(Transform t);
    2. // public delegate Vector3 Get(Transform t); // So klappt es
    3. private static Delegate statDelegate;
    4. public void Init()
    5. {
    6. PropertyInfo[] infos = typeof(Transform).GetProperties(BindingFlags.Public | BindingFlags.Instance);
    7. foreach (PropertyInfo info in infos)
    8. {
    9. MethodInfo method = info.GetGetMethod();
    10. var newType = typeof(Get<>).MakeGenericType(method.ReturnType);
    11. // Hier tritt der Fehler auf
    12. statDelegate = Delegate.CreateDelegate(newType, method);
    13. var obj = statDelegate.DynamicInvoke(this.Value);
    Dateien
    • DynamicGet.zip

      (62,49 kB, 35 mal heruntergeladen, zuletzt: )

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „LaMiy“ ()