Say I have a .NET class like so:
public class Person { public string Name { get; set; } public int Id { get; set; } }
Visual Studio has a nifty refactoring tool called Extract Interface that will extract an IPerson interface, and have Person implement it. Is there a way to do that programmatically from outside of Visual Studio? I’d even take a shell script if it can’t be done programmatically.
[EDIT] In actuality, I would have up to 50 classes, each with dependencies on each other.
Reflection would work, but it would introduce another wrinkle into the whole thing. The classes are already generated by xsd.exe. So, if I understand it correctly, the steps I would need to take would be:
- Generate classes from
xsd.exe. - Compile the classes on the fly so that I can use reflection.
- Reflect over them, emit the interfaces, and edit the original classes to implement said interfaces.
Generally I’d be in favor of just ditching the interfaces and using the classes directly, but for various reasons I cannot.
In a word: reflection.
It’s quite feasible to write a bit of code that takes a class object, reflects over its public methods, and writes a text file that’s the definition of an interface that the class implements.
However, it’s a bit more work to decide WHICH methods belong on an interface (e.g. a class may implement more than one, right?) and to add the correct notation to the class’s source code.