reflection

Converting an integer to a boxed enum type only known at runtime

Imagine we have an enum: enum Foo { A=1,B=2,C=3 } If the type is known at compile-time, a direct cast can be used to change between the enum-type and the underlying type (usually int): static int GetValue() { return 2; } ... Foo foo = (Foo)GetValue(); // becomes Foo.B And boxing this gives a box of type Foo: object o1 = foo; Conso...

Is there an easy (idiomatic) way to convert a java.lang.reflect.Method to a Scala function?

Can I retrieve a Method via reflection, somehow combine it with a target object, and return it as something that looks like a function in Scala (i.e. you can call it using parenthesis)? The argument list is variable. It doesn't have to be a "first-class" function (I've updated the question), just a syntactic-looking function call, e.g. f...

Any way to access the type of a Scala Option declaration at runtime using reflection?

So, I have a Scala class that looks like this: class TestClass { var value: Option[Int] = None } and I'm tackling a problem where I have a String value and I want to coerce it into that Option[Int] at runtime using reflection. So, in another piece of code (that knows nothing about TestClass) I have some code like this: def setField...

How do I get class of an internal static class in another assembly?

I have a class C in Assembly A like this: internal class C { internal static string About_Name { get { return "text"; } ... } I have about 20 such static properties. Is there a way, in an outside assembly, without using friend assembly attribute (.Net reflection only), get class C so I can invoke any of the static string pro...

Generically creating objects in C#

What I am trying to do is load in objects from an XML save file. The problem is those objects are configurable by the user at runtime, meaning i had to use reflection to get the names and attributes of those objects stored in an XML file. I am in the middle of a recursive loop through the XML and up to the part where I need to create a...

Delphi getting property value of a member from ClassType

I am implementing a Boilerplate feature - allow users to Change descriptions of some components - like Tlabels - at run time. e.g. TFooClass = Class ( TBaseClass) Label : Tlabel; ... End; Var FooClass : TFooClass; ... At Design time, the value Label's caption property is say - 'First Name', when the application is run, there ...

PHP get overridden methods from child class

Given the following case: <?php class ParentClass { public $attrA; public $attrB; public $attrC; public function methodA() {} public function methodB() {} public function methodC() {} } class ChildClass extends ParentClass { public $attrB; public function methodA() {} } How can I get a list of me...

Reflection: Get FieldInfo from PropertyInfo

Hi guys. I'm doing some dynamic code generation using Reflection, and I've come across a situation where I need to get the backing field of a property (if it has one) in order to use its FieldInfo object. Now, I know you can use .IsDefined(typeof(CompilerGeneratedAttribute), false); on a FieldInfo to discover whether it's autogener...

Get a string representation of a property in C# at run-time

I have seen the reverse of this question quite a few times, but have not seen how to do what I would like. Suppose I have the following code: var myNewData = from t in someOtherData select new { fieldName = t.Whatever, fieldName2 = t.SomeOtherWhatever }; If I wish t...

.Net Reflection - Calling unexisting methods

Lets say i call method M1 on class A using reflection. The method does not exist. Is there any way to put a handler on class A that says, "someone is trying to execute method M1"? Or Is it possible to add a method dinamically to a class? I want to add a method M1...Mn that always does MyStaticClass.DoAction("M1...Mn"); Something li...

Open generic interface types of open implementation don't equal interface type?

Here's a test that should, in my opinion be passing but is not. [TestMethod] public void can_get_open_generic_interface_off_of_implementor() { typeof(OpenGenericWithOpenService<>).GetInterfaces().First() .ShouldEqual(typeof(IGenericService<>)); } public interface IGenericService<T> { } public class OpenGenericWithOpenService...

Using Java Reflections to retrieve member classes

Hi All, I am using .getDeclaredClasses() method to retrieve all the classes that have been defined in object. However, I am not able to retrieve anonymous classes defined in the class. Here is the code sample that I am testing: public class TempCodes { public static void main(String[] args) { Ball b = new Ball() { pu...

InvalidCastException when creating an instance using assembly.CreateInstance

I'm looking for an explanation for the following - I have an assembly I'm loading using Assembly assembly = Assembly.LoadFrom(filename); I then loop on all the types in the assembly, and wish to try and find out if a type implements a particular interface and if so I want an instance of that type, I've tried several things which did ...

Method parameter values via introspection ?

Hello, How/am I able, using introspection or reflection get the parameters a method 1 level up in the call stack and it's values ? ...

Help me eliminate redundant code

I have an ASP.NET (3.5) page that allows a user to upload an Excel 2003 file with multiple sheets and that data is inserted into staging tables in the database. The mapping of database tables/columns to Excel sheets/columns is specified in an XML file. I use LINQ to SQL to send the data to the database. ImportTablesDataContext db = ne...

Mapping C structure to an XML element

Suppose I have a structure in C or C++, such as: struct ConfigurableElement { int ID; char* strName; long prop1; long prop2; ... }; I would like to load/save it to/from the following XML element: <ConfigurableElement ID="1" strName="namedElem" prop1="2" prop2="3" ... /> Such a mapping can be trivially done in Java/C...

Getting value of public static final field/property of a class in Java via reflection

Say I have a class: public class R { public static final int _1st = 0x334455; } How can I get the value of the field/property "_1st" via reflection? ...

Need to cast to an object without knowing what type the object is

I am trying to dynamically load my authentication server type based on a setting. I am hung up on how to cast to a type when I don't know the type. Type t = Type.GetType(WebConfigurationManager.AppSettings.Get("AuthenticationSvcImpl")); IAuthenticationService authCli = Activator.CreateInstance(t); return authCli.Authenticat...

Java: Easy way to get method stub out of class files within a JAR file? Reflection?

Hi, I'm searching for a way to get a list of method stubs of all classes within a jar file. I'm not sure where to start... May I use Reflection or Javassist or some other tools of which I've not heard yet!? At least it may be possible to unpack the jar, decompile the class files and scan with a line parser for methods, but I think that ...

C# - Recursive / Reflection Property Values

What is the best way to go about this in C#? string propPath = "ShippingInfo.Address.Street"; I'll have a property path like the one above read from a mapping file. I need to be able to ask the Order object what the value of the code below will be. this.ShippingInfo.Address.Street Balancing performance with elegance. All object gr...