mardi 31 mai 2022

Call Method by string value from data base

So I have a cell in my Oracle table, its value will be some System function or some Static Property like DateTime.Now.Year.
at the table cell the type is VARCHAR2 in .Net its string.

How can I produce a Generic Code for each case (Property, static Function) to get the value in the string

string str = "DateTime.Now.Year" // the value comes from DataBase
var valueFromDataBase = str.invoke();




Is it possible to use all possible types from reflection as generic type?

I am trying to use reflection to automatically create savedStateHandle for some classes.

fun <T> KProperty1<T, *>.argFrom(handle: SavedStateHandle) = 
    when (this.returnType.javaType) {
        String::class.java -> handle.get<String>(this.name)
        Int::class.java -> handle.get<Int>(this.name)
        Uri::class.java -> handle.get<Uri>(this.name)
        else -> throw RuntimeException("Type not implemented yet")
    }

As you see IF the type is for instance String then I want the return type of get function to be String and when doing it this way I have to define every single case manually.

It would be nice if I could just do something like this.

fun <T> KProperty1<T, *>.argFrom(handle: SavedStateHandle) =
    handle.get<this.returnType.javaType>(this.name)




lundi 30 mai 2022

Why class member property reflection in Kotlin?

In 'Kotlin in Action', it says "if a memberProperty refers to the age property of the Person class, memberProperty.get(person) is a way to dynamically get the value of person.age" with code(10.2.1 The Kotlin Reflection API):

class Peron(val name: String, val age: Int)
>> val person = Person("Alice", 29)
>> val memberProperty = Person::age
>> println(memberProperty.get(person))
29

I can't understand why this example refers to "dynamically" getting the value of property. It just works when I run this code:

println(person.age)

Is there any other case or example of member property reflection?





How to call a sub class using a string variable?

So I have this simple for loop and I want to access a variable of a class

However there are multiple classes so that's how I ended up with for loop.

This is what my code looks like.

for (int i = 0; i<itemId.Length; i++)
{
    Type type = Type.GetType(" " + itemid[i]);
    object instance = Activator.CreateInstance(type);
    Debug.Log(itemResponse.data.Invoke(itemid[i]).name);
}

I am trying to access

itemResponse.data."My String".name

The classes I want to access are.

public class _1001{ }
public class _1002{ }
public class _1003{ }

and so on, is there any way I could do that?

Thanks





vendredi 27 mai 2022

How to return either

Using generics and reflection to do a POST, then process the JSON, then return the correct type with its data.

Problem is that I won't know in advance if the type will be a single object or a list of objects. So my generic return type needs to be either a single thing, or a list of single things. (This is .NET Core 6, so what's why the ! in some places...)

How do I handle this conundrum?

    private static async Task<T> ProcessJsonResponse<TX>(HttpResponseMessage response, 
    string? returnModel = null)
    {
        var result = await response.Content.ReadAsStringAsync();

        if (returnModel != null)
        {
            var type = Type.GetType(returnModel);
            var obj = (T?)Activator.CreateInstance(type!);

            var test = type!.GetMethod("FromJson");
            object[] parametersArray = { result };
            var tim = test!.Invoke(obj, parametersArray);
            if (tim is List<T> nowwhat)
            {
               //can't return List<t> since the return type is T
            }
            return (T)tim!;
        }
        //<snip> other non-relevant code ...
    }




jeudi 26 mai 2022

What's the purpose of fromReflectedMethod and toReflectedMethod in JNI?

I was reading the JNI spec and saw the reflection support methods section.

I don't quite understand what they are used for or what's the intended purpose. Since method ID is looked up using name and signature, don't we already have that information? And if we want to get more details, we can always write a java wrapper. So that cannot be the purpose I thought.





mercredi 25 mai 2022

Golang get value of field on child struct using reflection [duplicate]

I'm trying to write a function on a struct that takes a parameter and returns the value of the field that the parameter points to. I then have several child structs that inherit from the root struct (the one with the function). When calling the method on one of those child structs to get a property that only that struct has, it throws the error panic: reflect: call of reflect.Value.Interface on zero Value. I have created a minimal reproduction here. Is there any way for me to only have the method on the root struct while still being able to get values from a child struct?

Code (same as playground):

package main

import (
    "fmt"
    "reflect"
)

type Parent struct {
}

func (p Parent) Get(name string) any {
    v := reflect.ValueOf(p)
    return v.FieldByName(name).Interface()
}

type Child struct {
    Parent
    Field string
}

func main() {
    child := Child{
        Field: "Hello",
    }
    fmt.Println(child.Get("Field"))
}