reflection

A way to use a string for creating new classes

I have a string with the name of a class, is there a way (writing in Java) to use that string when creating a new instance of that class, to do something like this: object obj = new dataInString. So the dataInString is parsed from the string. ...

Problem in the GetDeclaredMethods (java)

hello to all I have a small problem in my code I have 2 classes public class A { public A foo(int a) {return new A();} } public class B extends A{ public B foo(int x){ return new B();} } now in my code I want to print only the method that was declared in class B in this way B b = new B(); Method[] m = b.getClass().ge...

How do I determine which interface is referenced by an explicitly-implemented MethodInfo object?

I have a MethodInfo object that represents an explicitly-implemented interface method, as follows. MethodInfo GetMethod() { return typeof(List<>).GetMethod( "System.Collections.IEnumerable.GetEnumerator", BindingFlags.Instance | BindingFlags.NonPublic); } How do I query this MethodInfo object to obtain the interfac...

Java: Illegal Argument Exception

I'm getting an IllegalArgumentException, but I can't figure out why. The function I'm trying to access: private static Player checkEvents(Player[] players, GameMaster bananas) The problematic code: @Test public void testCheckEvents() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, I...

C# Reflection - changing the value of a field of a variable

Hello, I have an instance of a class, I want to change an object data member of this instance only with another object of the same type (swap), due to my system constraints i can't use =,new or setter operators. Basically I would like to change the value of a field of a variable, the field is an object contained inside another object ...

Extracting unique caller information in java

What I want to do is implement some basic security by checking not only what class has called a particular method, but also, which instance of that class. I tried StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace(); but that obviously only gives me the class name. The problem with allowing/requiring th...

Avoiding an ambiguous match exception

I am invoking a static method Parse on a type via reflection because I do not know the type of the object at compile-time (I do know, however, it has a Parse method, taking a string). However, I am getting an ambiguous match exception, presumably because there are a lot of overloaded Parse methods each taking a single object (string, in...

Dynamic typecasting in asp.net mvc Edit action: how can I handle multiple datatypes without throwing an exception?

I'm making a generic MVC viewer/editor for data objects I've designed, modeled after ActiveRecord. The data objects are completely generic, they can have primary keys that are strings, ints, int16s, whatever the database uses (the persistence layer at this organization uses all sorts of datatypes for primary keys and I want this MVC "br...

Column Sequence and the Entity Framework

So, when you bind a collection of Entity objects to a grid component, the grid displays those fields in the sequential order they are found in the SQL Table they came from. This shows that the ordinal position of the fields are associated with their corresponding entity properties... somehow or other. So here is the question: How can ...

Check Java Object Hierarchy Externally

Say I have the string "Bar" and I want to know whether or not Bar is a valid object, and further, whether "Bar" extends "Foo". Would I be using Java reflection or is there a better way, like a database? and how would I do it? Cheers ...

How garbage collected garbage the object which generated using reflection?

Hello All One of my friend told me that dynamic method is the only way if you want to at run time construct code that can be garbage collected. One question in my mind that how garbage collector garbage the object which generated using reflection? ...

ToString Extension method for FaultException/FaultException<>

Hi, I'm currently trying to create a ToString - extension method for FaultException, that can also deal with FaultException<>. The problem I have is, that I want to include the details without using reflection. What I currently have is: if (ex.GetType() == typeof(FaultException<>)) { var prop = ex.GetType().GetProperty("Detail"); ...

.NET Reflection: Find used types

I have been pulling my hair out trying to solve this. What I am attempting to do is to build a 'map' of how objects are being used in a bit of code I am working on. Think of it as an enhanced Find Usages. The easiest way to show this is by example: public class MasterClass { Type1 type1; Type2 type2; Type3 type3; void In...

Can I use reflection and a string to get/set the correct property of an object?

Hi All, I am using c# & .NET 3.5. A vendor gives us an object that has properties of UserVarNbr1, UserVarData1, UserVarNbr2, UserVarData2,... UserVarNbrN, UserVarDataN. Not the way I would have coded it but nonetheless, this is what we have to work with. Another object in the application returns a collection of items used to represent th...

Getting all fields - including inherited ones - with a custom attribute

I've got a custom attribute BacksCache that I'm using to mark fields in a class that need to be "defaulted" when certain conditions are met. I'm using the following code: Type type = obj.GetType(); FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.FlattenHierarchy); ...

What is the best way to instance of base class to instance of derived class in C# 4.0?

Now, I am working on ASP.NET MVC 2. I just found some serious problem about View Model class that derives from base class in Model project. Every time when I fetch data from database, I have to cast it to View Model instance that is not possible in most OOP language. Base class public class MyBaseClass { public string ID { get;set;...

C#: how to get an object by the name stored in String?

Good day, is it possible in C# to get an object by name? i.e. get this.obj0 using string objectName = "obj0"; executeSomeFunctionOnObject(this.someLoadObjectByName(objectName)); ...

DLR and reflection

Everywhere I read about the new DLR in .net 4, they say that a good use for it is reflection, and the code snippet always shown is something like dynamic d = GetSomeObject(); d.DoSomething(); d.SomeMember = 1; What does GetSomeObject() look like? I can't find anywhere that explains that. I understand that it can be anything, but in t...

new() with reflection on FieldType

I have some code that looks like else if (oField.FieldType.IsClass) { //var t = oField.FieldType.new() someObj.fill_data(t); oField.SetValue(o, t); } I dont know how to allocate var t. How might i do this? There no way for me to know what the type could be so writing FieldType.IsAssignableFrom(KnownType) can not be a worka...

How can i debug code that invoked with java reflection?

I hava an application which deployed on J2EE application server , inside the application there is implementation of class loader loading jar files into the JVM, inside those jar files there are Test Cases which implements common interface, by java reflection i dynamically invoke (by name) those test cases, the problem is that is like a b...