reflection

Best loose way to get objects with common base class

I struggled to come up with a good title for this question, so suggestions are welcome. Let's say we have an abstract base class ActionBase that looks something like this: public abstract class ActionBase { public abstract string Name { get; } public abstract string Description { get; } // rest of declaration follows } A...

C# equivalent of typeof for fields

With reflection, you can look up a class from a string at run time, but you can also say typeof(Foo) and get compile time type checking, auto completion etc. If what you want is a field not a class, you can look it up from a string at runtime, but if you want compile time type checking etc., is there anyway to say something like fieldof...

C# class type - How to determine whether it is a standard .net framework class

C# / .net framework What is the most reliable way to determine whether a class (type) is a class provided by the .net framework and not any of my classes or 3rd party library classes. I have tested some approaches The namespace, e.g. starting with "System." The Codebase of the assembly, where the dll is located All this "feels" a...

Use string value to create new instance

I have a few classes: SomeClass1, SomeClass2. How can I create a new instance of one of these classes by using the class name from a string? Normally, I would do: var someClass1 = new SomeClass1(); How can I create this instance from the following: var className = "SomeClass1"; I am assuming I should use Type.GetType() or someth...

Reflection: cast an object to subclass without use instaceof

I have this simple interface/class: public abstract class Message { } public class Message1 extends Message{ } public class Message2 extends Message{ } And an utility class: public class Utility { public void handler(Message m){ System.out.println("Interface: Message"); } public void handler(Message1 m){ Syste...

Dynamic, reflective SignalHandler in Java

How do I install signal handling logic iff sun.misc.Signal is available? Background First generation of my code, which assumed signal handling availability, looked something like this: class MyApp { public static void main(String[] args) { ... Signal.handle(term_sig, new SignalHandler() { public void han...

initializing properties with private sets in .Net

public class Foo { public string Name { get; private set;} // <-- Because set is private, } void Main() { var bar = new Foo {Name = "baz"}; // <-- This doesn't compile /*The property or indexer 'UserQuery.Foo.Name' cannot be used in this context because the set accessor is inaccessible*/ using (DataContext dc = n...

Is it possible to instantiate a Java Annotation given a Class<? extends Annotation>?

I have a Class<? extends Annotation> and tried calling newInstance() but Java yelled at me for the obvious reason that I can't instantiate an interface. But I know frameworks like EasyMock are perfectly capable of instantiating interfaces. What would it take to get a completely dumb Annotation instance out of my Class? ...

Get primitive, complex, ArrayEnumerable types

I have a separate class for each of my database entities and when I create an object of my class to reference the properties of a class it returns a circular reference which contains properties of other entities too that are related via FK ... to remove the circular reference I want to first make a copy of the object through "context pro...

What's an elegant solution to get the property values from two classes (that have the same property names) that do not use inheritance?

Essentially I have to use a poorly implemented web service maintained by other programmers. They have two classes that don't derive from a parent class, but have the same properties (Ughh...). So it looks like this in my web service proxy class file: public partial class Product1 { public int Quantity; public int Price; } publi...

Mysterious newInstance method

Hi, All. This question is about the newInstance method and the call of missing classes(libraries) in run time. First I show some code. library: axis.jar, dom4j.jar jdk1.5, windowsXP T.java import java.io.StringWriter; import org.apache.axis.utils.StringUtils; import org.dom4j.DocumentHelper; import org.dom4j.io.OutputFormat; imp...

Reflection Permission problems when using the GSON library in a applet.

I'm writing an Applet that makes some JSON-RPC calls. I'm using the Google JSON library (GSON) to cast the response JSON into a class. Thsi seems to work fine as is but when I use this code in my Applet, I'm hit with a java.lang.reflect.reflectpermission. From what I read on this thread on SO, it seems that since GSON uses Reflection, I...

Dynamic swappable Data Access Layer

I'm writing a data driven WPF client. The client will typically pull data from a WCF service, which queries a SQL db, but I'd like the option to pull the data directly from SQL or other arbitrary data sources. I've come up with this design and would like to hear your opinion on whether it is the best design. First, we have some data ob...

.NET Reflection: How to call method of interface without creating instance?

I have situation where I have to call method of interface using reflection, like this object x = null; MethodInfo method = interfaceExists.GetMethod("ShutDown"); method.Invoke(x, new object[] { 4 }) As you can see I do not create instance of object! And, as I can supposed, I receive exception Non-static method requires a target ...

Nullables? Detecting them

Ok, im still a bit new to using nullable types. I'm writing a reflecting object walker for a project of mine, im getting to the point where im setting the value of a reflected property with the value i've retrieved from a reflected property. The value i've retrieved is still in object form, and it dawned on me, since i want my object w...

Determining an object's variable name dynamically?

Let's say I have some objects: ArrayList<SomeObject> list = new ArrayList<SomeObject>(); SomeObject A = new SomeObject(); SomeObject B = new SomeObject(); SomeObject C = new SomeObject(); SomeObject D = new SomeObject(); These constructors automatically add each object to the ArrayList so I can iterate over them but still maintain the...

Where is System.CodeDom.Compiler.CompilerParameters in Silverlight ?

Dear All, I want to create Mathematics Expression Evaluator in Silverlight. To do that easily, I need compilation on the fly using System.Reflection, System.Reflection.Emit, System.CodeDom.Compiler, and other related assemblies. However, I cannot find this class available in Silverlight. Give me some suggestion please. Thank you in a...

IEnumerable<T> and reflection

Background Working in .NET 2.0 Here, reflecting lists in general. I was originally using t.IsAssignableFrom(typeof(IEnumerable)) to detect if a Property I was traversing supported the IEnumerable Interface. (And thus I could cast the object to it safely) However this code was not evaluating to True when the object is a BindingList<T>. ...

Tracing all events in VB.NET

I keep running into situations where I don't know what event I have to listen to in order to execute my code at the correct time. Is there any way to get a log of all events that is raised? Any way to filter that log based on what object raised the event? EDIT: Final solution: Private Sub WireAllEvents(ByVal obj As Object) Dim para...

Replacing instructions in a method's MethodBody

Hi, (First of all, this is a very lengthy post, but don't worry: I've already implemented all of it, I'm just asking your opinion, or possible alternatives.) I'm having trouble implementing the following; I'd appreciate some help: I get a Type as parameter. I define a subclass using reflection. Notice that I don't intend to modify th...