reflection

How to find all classes of a particular interface within an assembly in .net

I have a scenario whereby I want n amount of classes to look at the same data and decide if any work needs to be done. Work is done by a team, and multiple teams can work on the data at the same time. I was thinking of creating a class for every team that would implement the CreateWork interface. All CreateWork classes must have their sa...

Getting the type of a "Type" in C# reflection

This is harder to find in the docs than I imagined. Anyway, I have some instances of Type. How can I find out if they represent classes, methods, interfaces, etc? class Bla { ... } typeof(Bla).GetKindOrWhatever() // need to get something like "Class" (I'm using Mono on Linux but that shouldn't affect the question, I'm making portable...

Is Reflection breaking the encapsulation principle?

Okay, let's say we have a class defined like public class TestClass { private string MyPrivateProperty { get; set; } // This is for testing purposes public string GetMyProperty() { return MyPrivateProperty; } } then we try: TestClass t = new TestClass { MyPrivateProperty = "test" }; Compilation fails wi...

Python: Get list of all classes within current module

I've seen plenty of examples of people extracting all of the classes from a module, usually something like: # foo.py class Foo: pass # test.py import inspect import foo for name, obj in inspect.getmembers(foo): if inspect.isclass(obj): print obj Awesome. But I can't find out how to get all of the classes from the cu...

Cast Interface in generic method

Hello All I am new user of interface. My problem is i have create a Generic class which have two parameter one is object type another is interface type. Now within class i can cast object using Reflection but i am not getting the way to cast interface. ...

how know if a Type has inherited some other type ?

how know if a Type has inherited some other type ? Type t; // i get the t from somewhere bool b = t.IsInhertitedFrom(typeof(BaseType)); ...

Access the function object (closure) of a setter

Assume I have the following class: class Example { function set something(value:String):void { trace("set something"); } function doSomething():void { trace("something"); } } I can access the functions as objects like this: var example:Example = new Example(); var asdf:Function = example.doSom...

Getting attributes of Enum's value

Hi everyone, I would like to know if it is possible to get attributes of the enum values and not of the enum itself? For example, suppose I have the following enum: enum FunkyAttributesEnum { [Description("Name With Spaces1")] NameWithoutSpaces1, [Description("Name With Spaces2")] NameWithoutSpaces2 } What I want is given the enu...

C/C++: any way to get reflective enums?

I've encountered this situation so many times... enum Fruit { Apple, Banana, Pear, Tomato }; Now I have Fruit f; // banana and I want to go from f to the string "Banana"; or I have string s = "Banana" and from that I want to go to Banana // enum value or int. So far I've been doing this.. Assuming the enum is in Fruit.h: /...

Advanced topic of dynamic lazy loading of DLLs in silverlight application

It's a common sense when you are developing a big silverlight application that break it into many small components, to make the original XAP file relatively small and dynamically load the necessary DLLs from the server side on demand. An easy way is to download(From WebClient) the dll in the program in the runtime accroding to the URL s...

Load Assembly at runtime and create class instance

I have a assembly. In this assembly I have a class and interface. I need to load this assembly at runtime and want to create an object of the class and also want to use the interface. Assembly MyDALL = Assembly.Load("DALL"); // DALL is name of my dll Type MyLoadClass = MyDALL.GetType("DALL.LoadClass"); // LoadClass is my class object ob...

Is it possible to clone a ValueType?

Is it possible to clone an object, when it's known to be a boxed ValueType, without writing type specific clone code? Some code for reference List<ValueType> values = new List<ValueType> {3, DateTime.Now, 23.4M}; DuplicateLastItem(values); The partical issue I have is with a value stack based virtual instruction machine. (And Im too ...

how to dynamicaly call a method while repecting privacy

Using dynamic method calls (#send or #method) the methods visibility is ignored. is there a simple way to dynamically call a method that will fail calling a private method? ...

Reflection.Emit better than GetValue & SetValue :S

I've been told to use Reflection.Emit instead of PropertyInfo.GetValue / SetValue because it is faster this way. But I don't really know what stuff from Reflection.Emit and how to use it to substitute GetValue and SetValue. Can anybody help me with this ? ...

C# How to create Object name fron string variaable

Hello, I have windows form with one textbox(Pname) and one button. I have one class by name Player with no constructor. When user enters text inside textbox (e.g. John) and clicks button, system will create and an instance of a class Player with name of an object as John. Thanks, Jay ...

Getting all Classes from a Package

Lets say I have a java package commands which contains classes that all inherit from ICommand can I get all of those classes somehow? I'm locking for something among the lines of: Package p = Package.getPackage("commands"); Class<ICommand>[] c = p.getAllPackagedClasses(); //not real Is something like that possible? ...

Discover subclasses of a given class in Obj-C

Is there any way to discover at runtime which subclasses exist of a given class? Edit: From the answers so far I think I need to clarify a bit more what I am trying to do. I am aware that this is not a common practice in Cocoa, and that it may come with some caveats. I am writing a parser using the dynamic creation pattern. (See the bo...

private methods using .net reflection. why??

Hi I used reflection many times before on public methods but never realized that private methods can be invoked too. See the link I am still thinking why is that allowed in the first place? Isn't that going to break the rule of "private" being "private"? puzzled AJ ...

IL Emit for invoking a delegate instance?

Basically, I'm accepting an event name as a string, to get the EventInfo. Then, I'm discovering the event handler type and event argument type using reflection, creating a new delegate of that type (myEventHandler), and hooking it up with the event. When ever myEventHandler is invoked, I need to downcast and pass the arguments to the han...

using reflection for this, is there another way?

I have a list of keyvaluepair(string,string) first string is something like class.property, second string is the value to assign to that class.property. I'm currently looping through that list and by using reflection I set every value. it work, but my question is there a faster way to do this? ...