0

In Visual Studio, I can right-click a method from a compiled assembly and pick "Peek Definition" to see a list of method definitions, properties, enums, etc from that class. This definition has a [from metadata] qualification, indicating that this isn't actual source, but something generated from (I assume) reflected metadata.

My question is, what actually produces this code, and can I access it programmatically? My goal is to take a 3rd party assembly and generate dummy code from it. For example, given an assembly compiled from a class like:

public class TheirClass
{
    public TheirClass() 
    {
        // initialization magic
    }
    public string Name { get; private set; }
    public void SayHowdy() { Console.WriteLine("Howdy"); }
    private int Add(int x, int y) => x + y;
}

I want to generate minimal code like:

public class TheirClass
{
    public TheirClass() { }
    public string Name { get; }
    public void SayHowdy() => throw new NotImplementedException();
}

So, given that the "Peek Definition" feature in Visual Studio already does 90% of this, how can I access that tool programmatically?

superstator
  • 3,005
  • 1
  • 33
  • 43
  • `Is there an off-the-shelf way to achieve this?` ← There is nothing built into the .net framework to do this for you (*you would need to build it using reflection or find a library*). Also as far as [so] is concerned this falls under the close reason "asking for tool / off site resource...". – Igor Feb 10 '20 at 18:32
  • So you want to disassemble a third-party's application? Unless it has all the symbolic information the resulting output is usually very generic and takes a lot of work to wrangle it back into a form that is useful. Besides, there's the problem of the likely violation of contracts or licenses. – the Tin Man Feb 10 '20 at 18:32
  • You're correct, what is shown on a "from metadata" screen isn't real code. "From metadata" just means reflection was used to gather that information. You can do the same using reflection; I have no idea about getting the specific content you see in the "from metadata" page though. –  Feb 10 '20 at 18:32
  • Right, it's coming from reflection, and Visual Studio has that tool built into it somewhere to template that metadata into psuedo code. The question is, how can I use that tool outside the actual IDE UI? – superstator Feb 10 '20 at 18:34
  • I don't want to disassemble it exactly; the result doesn't have to run or be useful on its own. It just needs to provide an interface for other netstandard code to reference at compile time. – superstator Feb 10 '20 at 18:36
  • @theTinMan This doesn't require disassembly or symbols. This information is all available through the builtin System.Reflection API. – Mike Zboray Feb 10 '20 at 18:36
  • @superstator I feel like it would take less effort to write your own reflection-based code to generate this content than it would be to make use of the IDE's "from metadata" generation. Lots of people have done the former. I've never heard of anyone who has done the latter. I'm not suggesting it can't be done or that it has never been done, just that you're less likely to get that answer. –  Feb 10 '20 at 18:40
  • @Amy I guess that's the question. It doesn't look that hard, but that sets off the "then why can't I just make VS do it for me" alarm. – superstator Feb 10 '20 at 18:43
  • @Igor - genuine question then, what is the appropriate venue for this? It's certainly not a bug report that could go in a github issue. It's a pretty concrete question from my perspective; VS apparently has this feature, how can I use it? – superstator Feb 10 '20 at 18:45
  • 1
    For what its worth, I think you have a valid on-topic question, but in turn, I think your question is going to remain unanswered for a while. –  Feb 10 '20 at 18:47
  • Except for "`Is there an off-the-shelf way to achieve this?`" which is asking for information about possible other tools, which is off-topic. – the Tin Man Feb 10 '20 at 20:03
  • 1
    @theTinMan fair enough, I've edited that sentence. – superstator Feb 10 '20 at 20:07
  • 1
    From what I can tell this feature is called "Metadata as source" in the [roslyn codebase](https://github.com/dotnet/roslyn/blob/e704ca635bd6de70a0250e34c4567c7a28fa9f6d/src/EditorFeatures/Core/IMetadataAsSourceFileService.cs). However it looks like it is all internal. There are some [references](https://stackoverflow.com/a/16406425/517852) to it being public when roslyn was in CTP, but I guess it was removed from the public API surface. – Mike Zboray Feb 10 '20 at 20:31

0 Answers0