mardi 18 octobre 2016

Load Assembly and create class using Reflection

I could not find a way of creating a class object from given type and assembly definition using reflection. For my case, I have a string containing class and assembly name and need to create the object:

string str = "My.Assembly.Namesapce.MyClass, My.Assembly.Namesapce, Version=4.0.0.0, Culture=neutral, PublicKeyToken=84474dc3c6524430";
MyClass objClass = --SOME CODE TO USE  STR AND CREATE MyClass--

I have tried to split and create class but that is not very good way of doing it.





JVM - get hold of instance of a class in a running (non-instrumented) session

I need to stop a process by invoking a particular method on an instance of a given class. The process has been running for a day, and if I shut it down hard, I lose a lot of work. Due to a bug in the API, the GUI is wrongly wired and doesn't call the correct stop function. I have an interactive Java (Scala) interpreter attached, so if I could get hold of the instance of the process, I could easily invoke the correct method.

I am not in a debugging session, there is no com.sun.jdi. I can see the instance in a stack trace dump, but StackTraceElement doesn't contain actual instances, just classes and line numbers.

Is there any way in a running session, without specific instrumentation, to get hold of that instance -- through its class, through a thread dump?





lundi 17 octobre 2016

How to process a class in remote machine?

Usually we can use java reflection to get a class by simply doing "Class.forName("fully qualified class name")".

Now I have a class file (.class) located in client machine. For example, if the client machine is windows and the the class file myClass.class is located in c:\tmp\myClass.class. I have to "process" this class file in another machine in the server to know the class's methods etc.

Please do not ask why this is needed:-) Any suggestions on how to do it?





Getting class instance dynamically

I am trying to create instance of a class in my project. But I can't get it done. I searched stackoverflow and tried many methods but none has worked so far.

My directory structure is something like this:

Root
 Plugins
  AFolder
   AFolder.cs
 Controllers
  MyController

I want to create an instance of the AFolder.cs class in MyController but can't get it done.

I tried:

Object obj = Activator.CreateInstance("Root.Plugins.AFolder", "AFolder");

But this gives error: Could not load file or assembly.





How to cast object to EntitySet with variable generic

I have a variable (type: object) called o. I also have a variable (type: Type) called t. I know that o is an EntitySet with the type of t with a single entry in it.

I need to get the first element in the EntitySet so that I can use reflection on it to get it's propertys. Have tried a couple of approaches to getting the first element, but none have worked.

  1. Cast o to EntitySet - sounds good but dont know how to do this with t being a variable that can change
  2. Cast o to dynamic
  3. Cast o to EntitySet - The intention is that then I can use the methods of EntitySet and get first object but it throws an exception

Have:

Type t;    
object o; // we know it's EntitySet<t>

I need

t firstElement = o[0];





Get name of property's type, if this type is collection by reflection [duplicate]

This question already has an answer here:

I have simple class:

public class MyClass
{
    public List<string> MyProperty { get; set; }
}

Then, I want to get name of type of MyProperty, like this:

var name = typeof(MyClass).GetProperty("MyProperty").PropertyType.Name;

The problem is that variable name contains value List`1, while I want to get full name List.

How can I get full, correct name of property's type?





Cast to type which is not inherited

I have class lets say

public class Magic implements Serializable{
}

and this class does not inherit

public class Amount{
}

is there any way to cast Magic to Amount, i will be not accessing any fields what so ever, i just need Magic to "become" Amount? Is there any way?