Jump to content
  • Advertisement
Sign in to follow this  
Timberl

Interpretting a c# file

This topic is 4825 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'd like to traverse a c# class-model to automatically generate some code. Does anyone know any good packages or methods to be able to traverse a c# class model - what I'm looking to do is, loop through every class (approximately a .cs file) in my project, and if the class implements a particular interface then register it in an index normal c# reflection doesn't provide what I need as far as I'm aware my latest idea has been to find something which can load a .cs file into a data-model, so that I would expect to be able to say things like: foreach( CsClass c in (scan for .cs files) ) if(c.extends=="MyInterface"){ Indexer.add( c.targetDataObject, c.className ); } } where targetDataObject is defined in MyInterface and is the object that the class performs actions on... This is all so that I can easily find suitable classes to act upon a particular dataObject type ... any help would be much appreciated

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Timberl
normal c# reflection doesn't provide what I need as far as I'm aware

If you all you need is find types who implement a certain interface, reflection is more than sufficient. Something like this (pseudo-pseudo-code):

Assembly asm = this.GetType().Assembly; // only current assemhly, use .LoadFrom if you want other assemblies
Module module = asm.GetModules()[0]; // only single module assemblies, iterate over Modules if you need more
Type[] fooTypes = module.FindTypes( delegate(Type type, object val)
{
return typeof(IMyInterface).IsAssignableFrom(type);
}, null);

// types now contain an array of Type objects representing types implementing the IMyInterface type


Of course, if you're on .NET 1.x, you need to use a regular delegate instead of the anonymous one. Works the same, though.

Share this post


Link to post
Share on other sites
I'm not clear what you want:

Problem 1 - You want to compile .cs files at runtime and then use the new classes kind of like a runtime plug-in system?

Solution - Look at the Microsoft.CSharp.CSharpCodeProvider.CreateCompiler() function, and then use reflection on the resulting Assembly the same way that you would in Arild Fines' solution.


Problem 2 - You want to scan through the classes that you already compiled into your .exe or .dll and find every one that implements an interface?

Solution - use Arild Fines' solution.

Share this post


Link to post
Share on other sites
that looks brilliant, thanks

I didn't know you could reflect on an assembly, much better than messing with c# interpretters.





Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!