dimanche 15 mars 2020

Why does GetManifestResourceStream returns null (with dotnet core)

Using dotnet core: .NET Core SDK Version: 3.1.102

Why is the following returning null
?

typeof(MyClassName).GetTypeInfo().Assembly.GetManifestResourceStream("MyFile.cs")

I verified the file exists in solution and has in properties 'Embedded Resource'





samedi 14 mars 2020

Can I check that the method is jitted?

Can I check that the method is jitted? I want to know this in runtime. I see it like this:

MethodInfo mi = GetMethodInfo("method name");
var jitted = mi.IsJitted

Do you know C# has this functionality?





ArgumentException on method invocation with reflection: Object of type 'System.Object[]' cannot be converted to type 'T[]'

I'm trying to call a method with reflection. This is the class:

public class MyData<T> where T : struct
{
    public void CopyFrom(T[] array)
    { /* code */ }
}

Now I have an array that contains the objects that I want to put into the class, but my objects are boxed.

var container = new MyData<int>();
var method = typeof(MyData<>).GetMethod("CopyFrom");
object[] boxed = new object[] {1, 2, 3};
method.Invoke(container, new object[] { boxed });

The error that I get is this: System.ArgumentException: Object of type 'System.Object[]' cannot be converted to type 'T[]'.

What is the right way to call this method?

Note that this is not a generic method. So it's not duplicate of How do I use reflection to call a generic method?





ArgumentException on method invocation: Object of type 'System.Object[]' cannot be converted to type 'T[]' [duplicate]

I'm trying to call a method with reflection. This is the class:

public class MyData<T> where T : struct
{
    public void CopyFrom(T[] array)
    { /* code */ }
}

Now I have an array that contains the objects that I want to put into the class, but my objects are boxed.

var container = new MyData<int>();
var method = typeof(MyData<>).GetMethod("CopyFrom");
object[] boxed = new object[] {1, 2, 3};
method.Invoke(container, new object[] { boxed });

The error that I get is this: System.ArgumentException: Object of type 'System.Object[]' cannot be converted to type 'T[]'.

What is the right way to call this method?





Java Class based on Enum fileds

I'm dealing here with JWT and setting up custom fields. All those custom fields are described in Enum

public enum JwtFields {
    userId,
    manyOtherCustomFieldsBellow,
    blaBlaBla
}

so whenever I create token instead of passing strings for the keys, I'm using an enum as it is faster and safer.

Claims claims = Jwts.claims().setSubject(userId);
claims.put(JwtFields.someCustomFieldFromEnum.name(), "someValue")

Now, once I check that received token is valid and all necessary custom fields are present I want to deserialise it to some TokenDecoded class and attach it to request payload, so whenever I'm processing request I will have all values and fields from the JWT token.

However, that deserialise class pretty much contains all fields from Enum, and if tomorrow I will add new filed to enum, ill have to also manually update my TokenDecoded class to include that new custom filed too

The question: How can I make this TokenDecoded class to be based on enum fields, so if I add a new field to Enum, it will be automatically present in TokenDecoded. Is there reflection involved? or it could be achieved simpler?





How to edit method annotation attributes on runtime?

I have a simple annotation for editing keys of a map on runtime:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface UpdateKey
{
    String oldKey() default "";    //The key that needs to be replaced

    String newKey() default "";    //The new key

}

And i am using it on a method in some class:

public class CustomMap
{
    @UpdateKey(oldKey = "description", newKey = "productName")
    public void editMap(Map<String,String> map) throws NoSuchMethodException
    {
        //get the annotation values
        UpdateKey updateKey = this.getClass().getMethod("editMap").getAnnotation(UpdateKey.class);

        //save value and delete old key
        Object obj = map.remove(updateKey.oldKey());

        //set the value to the new key
        map.put(updateKey.newKey(),obj);
    }
}

But nothing seems to work to manipulate this annotation before calling the method, i already tried :

        // get the edit map method
        Method method = CustomMap.class.getDeclaredMethod("editMap");

        // get the annotation 
        UpdateKey updateKeyAnnotation = method.getDeclaredAnnotation(UpdateKey.class);

        Object handler = Proxy.getInvocationHandler(updateKeyAnnotation);

        //this is retrieving the reflection class fields NOT my annotation fields
        sout(handler.getClass().getDeclaredFields());

        Field f;

        //of course this will fail since there is no oldKey field in the reflection class
        f = handler.getClass().getDeclaredField("oldKey");  





vendredi 13 mars 2020

How to check if reflection worked or not?

i am using below method to check if Hotspot AP was enabled or not but there is a warning that says:

private boolean isApOn() {
    try {
        Method method = wifiManager.getClass().getDeclaredMethod("isWifiApEnabled"); // warning is here
        method.setAccessible(true);
        return (boolean)method.invoke(wifiManager);
    } catch (Throwable ignored) {
    }
    return false;
}

Accessing internal APIs via reflection is not supported and may not work on all devices or in the future

so there is my question that How can i check it if it worked or not?