reflection

Getting an assembly's Product property only having an AssemblyName

I'm getting the Product attribute from all the loaded assemblies in my application using: AssemblyProductAttribute product = (AssemblyProductAttribute)Attribute.GetCustomAttribute( assembly, typeof(AssemblyProductAttribute)); I would like to get this attribute for all the assemblies that the currently loaded assemblies ref...

How to get type of COM object

I am referencing a COM library in Visual Studio, so it has automatically created the corresponding Interop assembly for me. I would like to do a GetType() on these com objects, but they always return System.__ComObject. Querying them for an interface works though: bool isOfType = someComeObject is ISomeComObject; //this works But what...

C# Reflection: Is it possible to find an instance of an object at runtime?

I'm wondering if it's possible to use reflection to locate an object at runtime? This is more of an experiment than a practical requirement. I've used the .GetType() method on an object instance to do various things to the object, but my question is: what if I know an object of a certain type exists at runtime, but I can't refer to it b...

Best way to check if System.Type is a descendant of a given class

Consider the following code: public class A { } public class B : A { } public class C : B { } class D { public static bool IsDescendantOf(this System.Type thisType, System.Type thatType) { /// ??? } void Main() { A cValue = new C(); C.GetType().IsDescendantOf(cValue.GetT...

How can I get fields used in a method (.NET) ?

In .NET, using reflection how can I get class variables that are used in a method? Ex: class A { UltraClass B = new(..); SupaClass C = new(..); void M1() { B.xyz(); // it can be a method call int a = C.a; // a variable access } } Note: GetClassVariablesInMethod(M1 MethodInfo) returns B and C varia...

Workaround for Java Collections wrappers breaking reflection

Today, I discovered that using Collections.synchronizedXXX doesn't play well with reflection. Here's a simple example: import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Weird{ public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("Hello Worl...

Ruby: How do I get the methods of a class without having an object of it?

"abc".respond_to?(:sub) returns true, but String.respond_to?(:sub) returns false. The second returns false, because it asks whether objects of the class Class have a method sub, as String is an Object of Class. It is the same for methods()… How do I do these things and especialy respond_to?() without creating an Object of that class. ...

How to get a Class Object from the Class Name in Java

I know the class name, say "MyClass" and want to retrieve the Class object, ie. MyClass.class for future references. Is there a way to do that? I've looked through the web but most of the things I found related to it were about the ClassLoader, which I presume are not suitable for my case. I do not want to initialize a class, but only g...

How to make a generic MethodInvoke ???

Context I want to make this call via reflection instanceOfEventPublisher.Publish<T>(T eventInst); When I call ` private void GenCall(IEventPublisher eventPublisher, object theEventObj){ var thePublisher = eventPublisher.GetType(); thePublisher.InvokeMember( "Publish", BindingFlags.Default | B...

Setting value of BrowsableAttribute at runtime

I wanted to set a value of BrowsableAttribute for some of MyClass instance's properties at runtime: public class MyClass { [Browsable(true)] public int P1 { get; set } ... } Please advise how it can be done as well as how to add BrowsableAttribute to a MyClass instance's property at runtime if this attribute doesn't e...

Is this a decent sample implementation of javascript reflection?

This is for a job interview and I'd love it if someone could put an eyeball on this and say if I'm making any big mistakes. Here was the question: 1) Assume we're developing a JS-based debugger, similar to Firebug, and we need a way to inspect JS objects and elements. Write a routing that takes an object/element as input and...

Python Inspect - Lookup the data type for a property in a GAE db.model Class

class Employee(db.Model): firstname = db.StringProperty() lastname = db.StringProperty() address1 = db.StringProperty() timezone = db.FloatProperty() #might be -3.5 (can contain fractions) class TestClassAttributes(webapp.RequestHandler): """ Enumerate attributes...

Completely wrap an object in Python

I am wanting to completely wrap an object so that all attribute and method requests get forwarded to the object it's wrapping, but also overriding any methods or variables that I want, as well as providing some of my own methods. This wrapper class should appear 100% as the existing class (isinstance must act as if it is actually the cla...

Filter properties returned by TypeDescriptor.GetProperties() depending on the class they are declared in

Hey guys. I have the following situation. I want to use a TypeDescriptor to get the properties of a certain type. The type's depth in the inheritance hierarchy may vary. I only want to get the properties declared in the type itself and not in its parents (base). The problem is that when I call TypeDescriptor.GetProperties() it would ret...

Printing full object graph using C# and reflection

I have a compex object class A { int Field1; int field2; property ClassB ClassB; property classC classC; etc etc.... } I want to print the complete object graph using reflection. Any good code out there? ...

pass parameters in reflection C#

i have such code Bitmap b = new Bitmap(@"d:\0.bmp"); tessnet2.Tesseract ocr = new tessnet2.Tesseract(); ocr.DoOCR(b, Rectangle.Empty); the i try to make it through reflection Assembly a = Assembly.Load("tessnet2_32"); Type myType = a.GetType("tessnet2.Tesseract"); MethodInfo mymethod = myType.GetMethod("DoOCR"); Object obj = Activato...

Can tell ruby me if a given class wass defined in a given module.

Module M Class C end end What I need is something like: M.was_defined_here?(M::C) M.classes.include?(M::C) Does this exists somehow? I know I could parse M::C.name. But someebody could have the idea to change Module#name, to make it more astetic or something. I want a clean solution. ...

Get the original code passed into a PHP function as parameters using reflection

I'm wondering if there's any way to do the following: function whatever($parameter) { echo funky_reflection_function(); } whatever(5 == 7); // outputs "5 == 5" (not true or 1) I'm not optimistic, but anyone know if theres any crazy hack I can do to do this? ...

How to set custom "Host" header in HttpWebRequest?

How can I set a custom Host header in HttpWebRequest? I know that normally this class doesn't allow you to do so but is there anyway to use reflection or something like that without actually need me to send the whole packet with TCPClient? ...

Overriding a private method with Reflection

Is it possible to override a private method by using Reflection in .NET 3.5? ...