0

How to export class names,method names given a namespace in c# without it being a part of that namespace/code to preferrably a txt file. The code should not be a part of the code whose classes, method names are to be retrieved.

cooltoad
  • 171
  • 2
  • 12

1 Answers1

1

Just reflection:

string ns = "System.Text";

var types = from asm in AppDomain.CurrentDomain.GetAssemblies()
            from type in asm.GetTypes()
            where type.Namespace == ns
            orderby type.Name
            select type;
foreach(var type in types)
{
    Console.WriteLine("{0} ({1})", type.Name, type.Assembly.FullName);
    // and list the methods for each type...
    foreach (var method in type.GetMethods().OrderBy(x => x.Name))
    {
        Console.WriteLine("\t{0}", method.Name);
    }
}

Note that in the above I'm looking in all the loaded assemblies in the current app-domain; you can also just look in an individual assembly if appropriate.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • This code works as intended. Thanks a lot. Any leads on how to save it as a text file? – cooltoad May 07 '13 at 11:55
  • `using(var output = File.CreateText()) {...}` and write to `output` instead of `Console` ? – Marc Gravell May 07 '13 at 11:56
  • And can you brief on how to use individual assembly? – cooltoad May 07 '13 at 12:04
  • Since this answer explained and worked with the "System.Text" namespace i added reference to another project and gave its namesapce in ns. But not working. where am i going wrong?? – cooltoad May 08 '13 at 05:24
  • @cooltoad to use an individual assembly you could start with `from type in typeof(SomeTypeYouKnow).Assembly.GetTypes()`, where `SomeTypeYouKnow` is in the assembly you are talking about. The reason it isn't working currently is that nothing is *loading* the assembly you are talking about. The `typeof` will force it to be loaded. – Marc Gravell May 08 '13 at 06:30
  • I have exported it as a text file... But i also need to do the same in java. Any leads ? – cooltoad May 14 '13 at 08:01