reflection

copy an object to another object but only with the same fields

I would like to copy one object to another object, and the fields with the same names and type to be copied. Perhaps using reflections. e.g. object1.Name = object2.Name; object1.Age = object2.Age; However if object2.Address is not in object1 then it would be ignored and vis-versa. ...

Using unmanaged code, how can I find a type that has a given custom attribute assigned to it?

I had thought I could enumerate the types using IMetaDataImport.EnumTypeDefs and for each of the tokens returned, call IMetaDataImport.EnumCustomAttributes. This works, in so much as I get an array of mdCustomAttribute tokens. Using these tokens I can get a metadata token representing the Type of the returned custom attribute, by calli...

How do I invoke a MethodInfo that was created from an anonymous method?

In a previous question, I asked how to get a MethodInfo from an Action delegate. This Action delegate was created anonymously (from a Lambda). The problem I'm having now is that I can't invoke the MethodInfo, because it requires an object to which the MethodInfo belongs. In this case, since the delegates are anonymous, there is no own...

Attribute lost with yield

There is some code that I'm trying to convert from IList to IEnumerable: [Something(123)] public IEnumerable<Foo> GetAllFoos() { SetupSomething(); DataReader dr = RunSomething(); while (dr.Read()) { yield return Factory.Create(dr); } } The problem is, SetupSomething() comes from the base class and uses: Attribute.GetCu...

How can I retrieve the instance of an attribute's associated object?

I'm writing a PropertiesMustMatch validation attribute that can take a string property name as a parameter. I'd like it to find the corresponding property by name on that object and do a basic equality comparison. What's the best way to access this through reflection? Also, I checked out the Validation application block in the Enterpr...

Is there a way to loop through and execute all of the functions in a Python class?

I have class Foo(): function bar(): pass function foobar(): pass Rather than executing each function one by one as follows: x = Foo() x.bar() x.foobar() is there a built-in way to loop through and execute each function in the sequence in which they are written in the class? ...

Get compiler generated delegate for an event

I need to know what handlers are subsribed to the CollectionChanged event of the ObservableCollection class. The only solution I found would be to use Delegate.GetInvocationList() on the delegate of the event. The problem is, I can't get Reflection to find the compiler generated delegate. AFAIK the delegate has the same name as the event...

Get interface constant name using its value

This might not have a major usecase in projects ,but i was just trying a POC kind of project where in i get the key code,and using its value i want to print the key name on screen so i want to relive myself off writing switch cases ,so thinking of going by reflection. Is there a way to get the constant integer of interface's name using i...

How to work with varargs and reflection

Simple question, how make this code working ? public class T { public static void main(String[] args) throws Exception { new T().m(); } public // as mentioned by Bozho void foo(String... s) { System.err.println(s[0]); } void m() throws Exception { String[] a = new String[]{"hello", "kit...

What are the ramifications of using System.Reflection.Assembly.Load(System.IO.File.ReadAllBytes(path)) in T4?

System.Reflection.Assembly.Load(System.IO.File.ReadAllBytes(path)) So this is a work around to not being able to use T4 to reflect or read other files in the project or solution without locking the binaries from this post. The comments imply a memory issue. The comments talk about a no-unloading downside, would this be garbage collect...

How to use one sessionFactory, and create multiple named instances attaching to different DB's

I am trying to pass strings to my build Factory function. One string would be the path and name of the EO.dll. The other would be the path and name of the Map.dll. When I was hard coding the class name this worked for me. //// this was the origonal //m.HbmMappings.AddFromAssemblyOf<Add...

How can I retrieve the source file of a compiled method?

Hi all, I'm really stumped by this one! The StackFrame object (MSDN Link) has a GetFileName method that returns the original path of the source file that compiled the executing method (provided symbols were generated and included with the executing assemblies). It looks like this information is used to generate full exception text. I...

Dynamically invoke a dll in sharepoint

How to invoke a dll that is dynamically created (during runtime) in a sharepoint site? i am recieving an error that i dont have the load permissions. Any help on this will be grateful. ...

Unable to cast lists with Reflection in C#

I am having a lot of trouble with Reflection in C# at the moment. The app I am writing allows the user to modify the attributes of certain objects using a config file. I want to be able to save the object model (users project) to XML. The function below is called in the middle of a foreach loop, looping through a list of objects that ...

How can I restrict reflection for certain assemblies?

In my .net application I want to restrict reflection to certain assemblies. I mean I want that my a particular assembly can be reflected by only some predefined assemblies not by any other than that. How can i do that? Edit: This tool completely shuts down .NET disassembly and decompilation of an assembly. I want to allow some predefin...

Getting the Assembly Qualified Name of a class in Visual Studio

Hello, I'm writing a customized reflective library for some specialized custom domain logic, and that library is going to use XML configuration files that will dynamically resolve System.Type objects at runtime. However, when writing the XML configuration files, it's a bit of a pain to write the types because they need to be fully qual...

Reflection, get DataAnnotation attributes from buddy class.

Hi. I need to check if property has specific attribute defined in its buddy class: [MetadataType(typeof(Metadata))] public sealed partial class Address { private sealed class Metadata { [Required] public string Address1 { get; set; } [Required] public string Zip { get; set; } } } How to che...

Python: Is there a way to reflectivly list all attributes of a class

Given a class such as class MyClass: text = "hello" number = 123 Is there a way in python to inspect MyClass an determine that it has the two attributes text and number. I can not use something like inspect.getSource(object) because the class I am to get it's attributes for are generate using SWIG (so they are hidden in .so :)...

Identifying all types with some attribute

Hello SO; I have an issue with .Net reflection. The concept is fairly new to me and I'm exploring it with some test cases to see what works and what doesn't. I'm constructing an example wherein I populate a set of menus dynamically by scanning through my Types' attributes. Basically, I want to find every type in my main namespace that ...

Can't access CodeBase from a dynamically generated assembly.

I'm trying to create an assembly dynamically in .Net. I can't seem to figure out how to get the CodeBase property to return a value, however. Here's an example: var assemblyName = new AssemblyName { Name = "Whatever", CodeBase = Directory.GetCurrentDirecto...