reflection

How to determine using reflection the generic parameter of the base class..

I have the following structure public class MyClass : MyBaseClass<System.Int32> { } In a static method and without instantiating a new MyClass instance how do I get the type of the generic parameter used to build the concrete base class? e.g in the above example System.Int32 ...

C# reflection: check if a class is derived from a generic class

I have a generic class in my project with derived classes. public class GenericClass <T> : GenericInterface<T> { ...... } public class Test : GenericClass <SomeType> { } Is there any way to find out if a Type object is derived from the GenericClass ? t.IsSubclassOf(typeof(GenericClass<>)) is not working. ...

How do I get the subclass Type object in a static superclass function in .net, using reflection?

Ok, so I'm trying to make a nice superclass for data-access objects that can generate a tsql query to search all of the subclass's public string properties. I want to use reflection to get the type of the subclass and then iterate through all of the public string properties on the object, since those property names are the same as the d...

Can reflection ever fail in C#?

We have an application which we are developing in C#. We store all resources in a central resource DLL, the resource contains images, icons and strings from which we support multiple cultures. We access the resources from any project in the solution using the following code; private ResourceManager ResMan; ResMan = new ResourceManager(...

How do I determine if an object implements a method in Perl?

I've got a polymorphic array of objects which implement two (informal) interfaces. I want to be able to differentiate them with reflection along the lines of: if (hasattr(obj, 'some_method')) { # `some_method` is only implemented by one interface. # Now I can use the appropriate dispatch semantics. } else { # This must be th...

Type.GetType() for a class that resides in an unreferenced assembly

GetType() returns null when the type exists in an unreferenced assembly. For example, when the following is called "localType" is always null (even when using the full namespace name of the class): Type localType = Type.GetType("NamespaceX.ProjectX.ClassX"); I don't see any reason why Type.GetType shouldn't be able to retrieve a type...

Methods, Interfaces, Reflection and a difficult OOP design

I'm not quite sure how to explain this (that's why the title is kinda weird) but I'll have a go. Basically I'm doing some Oject-Oriented Design and I want to represent various different types of object, each of which can have various actions which it can perform. An example might help: things such as a File which can have delete, rename ...

Casting to abstract class or interface when generics are used

I have this method Verify_X which is called during databind for a listbox selected value. The problem is the strongly typed datasource. I want to use the abstract class BaseDataSource or an interface to call the methods supported: Parameters[] and Select(), Instead of using the most specific implementation as seen below. This is so on...

Type.IsSubclassOf() doesn't work across AppDomains?

I'm having some problems with the following code: private class ClientPluginLoader : MarshalByRefObject { public bool IsPluginAssembly(string filename) { AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += new ResolveEventHandler(CurrentDomainReflectionOnlyAssemblyResolve); Assembly asm = Assembly.Reflectio...

How to obtain a property value on descendant class

I have a class (Descendant1) that inherits from a base class (BaseClass). An instance of a descendant class is passed into a method that takes BaseClass as a parameter. Then using reflection, it calls a property on the object. public class BaseClass { } public class Descendant1 : BaseClass { public string Test1 { get { return "te...

.NET Load assemblies at runtime Again

I posted a similar question a time ago. I need to load an assembly at runtime. This is easy if I know the absolute path of the dll at runtime. But I dont :( The assembly.Load() or LoadFromFile() fails if the file is not in the application root. The only thing I have is the dll name. The dll could be located in the root, system32 or in...

How would you improve this shallow copying class?

I've written a class with a single static method that copies property values from one object to another. It doesn't care what type each object is, only that they have identical properties. It does what I need, so I'm not engineering it further, but what improvements would you make? Here's the code: public class ShallowCopy { publ...

How to programmaticaly import Java class

Is there a way in Java to programmatically import a class given its full name as a String (i.e. like "com.mydummypackage.MyClass")? ...

Use reflection to set a property value to Nothing (Null)

Edit: Based on the answer from LoveMeSomeCode, I believe this issue only appears in VB.Net. I'm trying to revert a class to a previous state by saving the old values of changed properties in a dictionary and setting them via reflection when I need to revert. I'm having a problem where if the old value is Nothing (null) I get a null refe...

Can I discover a Java class' declared inner classes using reflection?

In Java, is there any way to use the JDK libraries to discover the private classes implemented within another class? Or do I need so use something like asm? ...

Dynamically getting default of a parameter type

Question I am trying to dynamically get the default for a type that is specified in a ParameterInfo. _methods[methodName] returns a MethodInfo object. Unfortunately, the compiler doesn't like the "paramType" bit inside the default(paramType). I'm stumped. Error The type or namespace name 'paramType' could not be found (are you mis...

How do I create and access a new instance of an Anonymous Class passed as a parameter in C#?

I have created a function that takes a SQL command and produces output that can then be used to fill a List of class instances. The code works great. I've included a slightly simplified version without exception handling here just for reference - skip this code if you want to jump right the problem. If you have suggestions here, though...

Accessing object properties from string representations

I've got a custom object (example only code for ease of understanding) ... public class MyClass { private string name; private int increment; private Guid id; public string Name { get { return name; } set { name = value; } } public int Increment { get { return increment; } ...

Does GetCustomAttributes() preserve the attribute order in .NET?

The title pretty much says it all. When I'm doing some reflection through my classes, will the MemberInfo.GetCustomAttributes() method preserve the order of attributes on a member, or not? The official documentation does not say anything one way or the other. In case you're wondering why I would need this, here's the full explanation....

Help with annotations

Edit--@Uri correctly pointed out that this was an abuse of annotations; trying to actually create the menu data itself in annotations is just silly. They are good for binding however, I think I'll stick with using them to link the text data to the methods (the @Menu ("File") portion) since it's more explicit and flexible than reflecting...