dimanche 19 février 2017

How to pass the name of variable to a function and return its value in C#?

i have a case where i have several variables with similar names but not in an array. What i want to do is to pass the names of the variables to some function that i create and that function would return the value of the variable that was passed to the function by its name.

For example:

int valueByName(int x, string variableName){
   string newVar;
   string numString = x.ToString();
   newVar = variableName + numString;

   //and here i should get the value of newVar and return it to use it. but how?
   return valeuof(newVar);???
}

int num1 = 1;
int num2 = 2;
int num3 = 3;
string varName = "num";

for(int i = 1; i < 4; i++){
   Console.WriteLine(valueByName(i, varName));
}





samedi 18 février 2017

Can't access filed from xaml template through FieldInfo

I have xaml template for my MainWindow class:

...    
<Button x:Name="button1" Content="Button" Click="button1_Click"  />
<Label x:Name="superLabel" Content="Super content!" />
...

I wanna access superLabel field through relfections, after button click, like this:

public void button1_Click(object sender, RoutedEventArgs e)
{
  Type t = typeof(MainWindow);
  FieldInfo test1 = t.GetField("superLabel"); //  test1 == null
  FieldInfo test2 = t.GetRuntimeField("superLabel"); // test2 == null
  ...
}

but I am getting null in every test...





Make a class act as if it was inside the Android package to access hidden and package methods

I'm trying to improve a TextView performance by removing everything that I won't use: RTL, spell checker, error popup, XML attributes, unused input types, etc, and eventually the drawing part. I "cleared" the code to remove those but now I realize some methods and classes and hidden (@hide) and can't be accessed.

I see two solutions: adding the missing class to my package, or using reflection to get it. First method can't work because most added classes also tries to access hidden methods, so I need to add them too, I'll end up having to import the whole Android source code which is nonsense. That leaves me with reflection, lots of it. Is it really my only choice? (seems like a hacky one)

Is there something I can do to make the class act as if it was inside the Android package, being able to access any of its methods?





How to generate a direct calling wrapper bytecode with Reflection?

I use reflection in an Aspect Oriented Library project in C#. In some places, it gets slower. Then I read an article and it says "You can use reflection once in a speeded up way with generate a direct calling wrapper bytecode." What's the way to do this? What is unusual with this technic?





Java set "classLoader" field in class object

I knew the isAssignableFrom() method also check the class loader.

So I tried set classLoader field in class object but failed.

Is there a trick for this?

I want to make the originalClass.isAssignableFrom(transformedClass) is true.

The transformed class has the same name as the original class and package.





Java transformed class isAssignableFrom

I transformed specific class using ASM.

Changed only method's code. everything else is the same (ex class name, package..)

and I have two class object.

Object original;

Object transformed;

I tried original.isAssignableFrom(transformed) but it is false.

What does isAssignableFrom compare based on?

How do I get true from isAssignable()?

The reason for this is that an exception is thrown when using reflection.

sun.reflect.UnsafeFieldAccessorImpl.ensureObj seems use isAssignableFrom().

Exception stacktrace:

[22:15:29 WARN]: java.lang.IllegalArgumentException: Can not set final org.bukkit.command.SimpleCommandMap field org.bukkit.plugin.SimplePluginManager.commandMap to org.bukkit.plugin.SimplePluginManager
[22:15:29 WARN]:        at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
[22:15:29 WARN]:        at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
[22:15:29 WARN]:        at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(Unknown Source)
[22:15:29 WARN]:        at sun.reflect.UnsafeQualifiedObjectFieldAccessorImpl.get(Unknown Source)
[22:15:29 WARN]:        at java.lang.reflect.Field.get(Unknown Source)
[22:15:29 WARN]:        at com.github.lyokofirelyte.VariableTriggers.Manager.VTRegistry.registerCommands(VTRegistry.java:37)
[22:15:29 WARN]:        at com.github.lyokofirelyte.VariableTriggers.VTSetup.start(VTSetup.java:145)
[22:15:29 WARN]:        at com.github.lyokofirelyte.VariableTriggers.VariableTriggers.onEnable(VariableTriggers.java:51)
[22:15:29 WARN]:        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:271)
[22:15:29 WARN]:        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:316)
[22:15:29 WARN]:        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:407)
[22:15:29 WARN]:        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.enablePlugin(CraftServer.java:378)
[22:15:29 WARN]:        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.enablePlugins(CraftServer.java:328)
[22:15:29 WARN]:        at net.minecraft.server.v1_11_R1.MinecraftServer.t(MinecraftServer.java:437)
[22:15:29 WARN]:        at net.minecraft.server.v1_11_R1.MinecraftServer.l(MinecraftServer.java:398)
[22:15:29 WARN]:        at net.minecraft.server.v1_11_R1.MinecraftServer.a(MinecraftServer.java:336)
[22:15:29 WARN]:        at net.minecraft.server.v1_11_R1.DedicatedServer.init(DedicatedServer.java:276)
[22:15:29 WARN]:        at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:603)
[22:15:29 WARN]:        at java.lang.Thread.run(Unknown Source)





vendredi 17 février 2017

Reflexively instantiate class with constructor which takes Enum

I have a superclass OpCode and subclasses Add/And..etc

I am trying to reflexively construct the subclasses based on their class name string. The constructor takes an enum defined in the superclass.

I am trying:

OpCode theOpCode = (OpCode) Class.forName("com.sstangle.opCodes." + opCodeName).getConstructor(OpCode.OpEn.class).newInstance(opEn);

which results in

java.lang.NoSuchMethodException: com.sstangle.opCodes.Add.<init>(com.sstangle.opCodes.OpCode$OpEn)
    at java.lang.Class.getConstructor0(Unknown Source)
    at java.lang.Class.getConstructor(Unknown Source)

How do I correctly access the constructor taking the enum?

Thanks