0

Currently I'm trying to programmatically, File.ReadAllText a C# (.cs) file and convert it to a Type object so I can access the Type.GetMethods() function (and the rest of the functions). This .cs file is NOT in the same project, which is why I cannot just call the class in the code itself.

Only issue is I cannot find the way to do this. I've tried the following below by converting a String to a Type Object with the TypeDescriptor, but it failed with an error.

Code:

string code = File.ReadAllText(PathToHomeControllerFile);
Type t = (Type)TypeDescriptor.GetConverter(typeof(String)).ConvertTo(code, typeof(Type));

string methods = string.Join(",", t.GetMethods().Select(p => p.Name));
Console.WriteLine(methods);

File I'm trying to convert (HomeController.cs):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TestApplication.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult TestAction() 
        {
            return View();
        }
    }
}

Error:

'StringConverter' is unable to convert 'System.String' to 'System.Type'.

How do I achieve my goal?

Kevin Jensen Petersen
  • 423
  • 2
  • 22
  • 43
  • 3
    Well you'd need to compile the code - which is far from trivial, given that you need to know which other assemblies to reference etc. – Jon Skeet Mar 22 '18 at 22:34
  • @JonSkeet Is it possible to compile the code programmatically? If so, how? :) – Kevin Jensen Petersen Mar 22 '18 at 22:37
  • It's possible, but don't expect it to be a one-liner. There are *lots* of resources online around this - I suggest you search for a tutorial for Roslyn. – Jon Skeet Mar 22 '18 at 22:37
  • @JonSkeet Hmm.. Is there really no other way around this? What I basically only want is just the information about the functions (Name, return type and input parameters, that's it) – Kevin Jensen Petersen Mar 22 '18 at 22:38
  • So at the very least you need a C# *parser*. No, that's not trivial. You may be able to use Roslyn with less extra information than for a full compilation, but it's not simple. – Jon Skeet Mar 22 '18 at 22:41
  • @KevinJensenPetersen If all you want is the information on the methods, than don't read in the C# file - build it into a DLL, [generate an XML description of it](https://ewsoftware.github.io/XMLCommentsGuide/html/57C91630-95D6-4E3E-AF24-3415CC569AC8.htm), and then read the XML description in instead. However, I would like you to explain what you are actually trying to achieve, because this may or may not work for you depending on your constraints. – RB. Mar 22 '18 at 22:43
  • 1
    @RB. Well the use-case is in a VSIX Extension where I need to get information about all the MVC Controllers so I can create a report about these information everytime the custom button of the VSIX Extension is clicked, so building it into a DLL is kinda out of the question. – Kevin Jensen Petersen Mar 22 '18 at 22:45
  • Why can't you use the `Projects` collection and get the `CodeModel.CodeElements` collection for each project and analyze that? [Here](https://msdn.microsoft.com/library/8538545a-15c0-479d-9223-626030a5c6b9) is a MS article on doing it in VB. – NetMage Mar 23 '18 at 00:03
  • @NetMage This was exactly what I needed. I wen't on and found an answer on Stackoverflow based on CodeModel.CodeElements and found the solution here: https://stackoverflow.com/questions/32791188/visual-studio-extension-get-all-classes-and-interfaces-metadata – Kevin Jensen Petersen Mar 23 '18 at 02:00

1 Answers1

0

Since I was working with VSIX Extensions, I could simply go with CodeModel.CodeElements as stated in the comments on the original post. I found an answer by another fellow Stackoverflower and that did the trick.

Visual Studio Extension get all classes and interfaces metadata

Kevin Jensen Petersen
  • 423
  • 2
  • 22
  • 43