reflection

How do I call a field accessor in Scala using Java reflection?

If I have a small Scala class with a private field and public accessors: class Entity { private var _name:String = "" def name:String = <some stuff> def name_=(v:String) = <some stuff> } How can I invoke these accessors using Java reflection? The class may be 3rd party code, or at least really hard to change. Ple...

Use reflection to get attribute of a property via method called from the setter

Note: This is a follow-up to an answer on a previous question. I'm decorating a property's setter with an Attribute called TestMaxStringLength that's used in method called from the setter for validation. The property currently looks like this: public string CompanyName { get { return this._CompanyName; } [Tes...

Determine if Equals() is an override?

I have an instance of Type (type). How can I determine if it overrides Equals()? ...

Select Right Generic Method with Reflection

Hi I want to select the right generic method via reflection and then call it. Usually this is quite easy. For example var method = typeof(MyType).GetMethod("TheMethod"); var typedMethod = method.MakeGenericMethod(theTypeToInstantiate); However the issue start when there are different generic overloads of the method. For example the ...

How Do I Find The Owning Assembly Of An Object In C#

I'm trying to do some processing on all assemblies that own forms that are currently open in my application. I can easily get the form objects with: System.Windows.Forms.Application.OpenForms I want to iterate through this list and find the owning assembly for each instance. I know how to find the assembly that owns a given form cla...

Reflection failed for properties on anonymous types in Silverlight

Hi, I'm using Silverlight 4 with VS 2010 and trying to do reflection on anonymous type and I got some "Attempt by method '...' to access method '...' failed.". I tried various workarounds for this but I couldn't find and simple ones. class.CallAnonymous("SimpleClass", "HelloFunc", new { strIn = "Boo" }); public void CallA...

C# generics constraint: must define an overloaded operator

Possible Duplicates: Solution for overloaded operator constraint in .NET generics Define a generic that implements the + operator Hi, So I have a generic class MyClass<T> and I want to put a constraint that the type T must define an operator, for example public static T operator +(T x,T y). Is that possible? If not, I was...

Create a generic list using reflection

I have a function that uses reflection to set properties of object A from object B. At one point, I need to instantiate a generic collection. However, I am unable to get it working. Here is what I have now: IList list = destProperty.PropertyType.GetGenericTypeDefinition() .MakeGenericType(destProperty.PropertyType.GetGen...

Fieldinfo.FieldType.FullName is not giving datatype for datetime and double

Hi, I am using Fieldinfo.FieldType.FullName to get the field datatype. For a string i get System.String but for a Double i get System.Nullable`1[[System.Double, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089] similarly for DateTime i get System.Nullable`1[[System.DateTime, mscorlib, Version=2.0.0.0, Cult...

getDeclaredField(String) vs. getMethod(String) for private fields in a bean

I have a bean whose properties I want to access via reflection. I receive the property names in String form. The beans have getter methods for their private fields. I am currently getting the field using getDeclaredField(fieldName), making it accessible by using setAccessible(true) and then retrieving its value using get. Another way t...

Implementing INotifyPropertyChanged via reflection

Using C#/.Net 4.0, I'm storing data in a BindingList where dataRow is being defined at run time via Reflection.Emit. (The structure of the incoming data varies and is defined by an external source.) After struggling a bit with my first foray into the world of reflection and IL, I have been able to create my dataRow fill it with values, f...

How to determine and check whether a type in assembly is Custom type or Primitive type using reflection in .NET?

Is it possible to check at runtime whether given type is Custom data type or one of primitive data types of .NET? I have defined user defined types in assembly and those all types are some structs. I need to call the methods of user defined types of whome parameters are those structs. So this needs to fill the data accordingly before c...

.NET: Get Resources by Reflection

How do I get all th resources dynamically from my current assembly? I have tried two methods, GetManifestResourceNames and GetResourceSet with no success. I am comfortable with solutions in VB.net or C#. First Method This first method only returns an array of length 1 with this value "MyNameSpace.Resources.resource". The problem is tha...

Python's getattr gets called twice?

I am using this simple example to understand Python's getattr function: In [25]: class Foo: ....: def __getattr__(self, name): ....: print name ....: ....: In [26]: f = Foo() In [27]: f.bar bar bar Why is bar printed twice? Using Python 2.6.5. ...

Problem : Need to make COM InterOp at runtime using reflections Passing Pointers as parameters?

Hello, I need to make COM IntetrOp at runtime using reflections. My native COM Object's exposed methods have some parameters as pointers (DWORD*) and some double pointers (DWORD**) and some are user defined types(e.g SomeUDTType objSmeUDTType) and vice versa its pointer(i.e. SomeUDTType *pSomeUDTType). Now for dynamic method invocation...

Class extending - best practice/best solution

First thing to note - I KNOW DELEGATION AND DECORATOR PATTERNS! Second - I am using C# .NET 4.0, so if you come up with a solution that is specific for it, that's fine. But if solution will work for any OOP language and platform, that would be great. And here the question goes... I have a partial class (lets name it Class1), which I c...

Checking if a type supports an implicit or explicit type conversion to another type with .NET.

Imagine you've been given two System.Type's and you want to determine if there is an implicit or explicit type conversion from one to the other. Without specifically checking for the static methods is there a built in method to determine that the type supports either or these conversions? I know this is a brief body to a question but ...

How can I configure Unity from multiple versions of the same assembly?

I have multiple versions of an assembly that each implements a type called RequestHandler (with IRequestHandler). I want to configure unity each of the versions available using an alias like 'v1.1' or 'v1.2'. At runtime requests are handled by the correct version using the alias to create an instance of the correct version of the assem...

vb.net Getting a reference to a class by its name

Hello, I'm trying to use reflection to get the instance of a class in vb.net. I have a class 'A' in my web project and to test it, i create a new aspx page and try to write the following: Dim t as Type = Type.GetType("A") This returns "Nothing". But if i do this: Dim inst as A = new A() Dim t as Type = inst.GetType() t's type is "...

How can I access an explicitly implemented method using reflection?

Usually, I access a method in reflection like this: class Foo { public void M () { var m = this.GetType ().GetMethod ("M"); m.Invoke(this, new object[] {}); // notice the pun } } However, this fails when M is an explicit implementation: class Foo : SomeBase { void SomeBase.M () { var m = this.GetTy...