I am using ICSharpCode to decompile source code to send over the wire to a server via gRPC for long-running computations. These simulations implement a base class Optimization
from a shared library that is available both on the client and server. For example:
class CustomOpt : Optimization
{
protected override double Run()
{
for (int x = 0; x < 10000000; x++)
{
//do some long-running calculations
}
return double.PositiveInfinity;
}
}
I can decompile CustomOpt
just fine by passing the type into this method:
public static string GetSourceCode(Type type)
{
var location = Assembly.GetEntryAssembly().Location;
var decompiler = new CSharpDecompiler(location, new DecompilerSettings());
var customType = decompiler.TypeSystem.FindType(type);
var source = decompiler.DecompileAsString(customType.GetDefinition().MetadataToken);
return source;
}
This works great until the custom type references another type that is not part of .NET or the shared library. For example:
class SomeHelperClass
{
public void DoSomething() { }
}
class CustomOpt2 : Optimization
{
protected override double Run()
{
var helper = new SomeHelperClass();
return double.NaN;
}
}
If I decompile CustomOpt2
to source using the GetSourceCode(Type type)
method above, ICSharpCode returns
private class CustomOpt2 : Optimization
{
protected override double Run()
{
SomeHelperClass someHelperClass = new SomeHelperClass();
return double.NaN;
}
}
But the remote server complains it doesn’t know anything about SomeHelperClass. This is a long-winded setup to ask how can I retrieve all types that are referenced within a type? Essentially, I need recursive decompilation all the way down the inheritance chain until I reach a class that is part of .NET or the shared library. That way I can send source code over the wire for SomeHelperClasss
so that the server can compile CustomOpt2
.
Calling IType.GetMembers()
seems like a logical place to start but I’m stuck. Can anyone offer suggestions on how to approach this? Thank you!