简体   繁体   English

是否可以在运行时检索包含类的文件?

[英]Is it possible to retrieve the file containing a class at runtime?

I would like to know if it's possible to retrieve the .cs file that contains a certain class at runtime. 我想知道是否可以在运行时检索包含某个类的.cs文件。

Ex. 防爆。 I have the "Type" that represents my class (public class Foo) and from that, I would like to retrieve the path to its parent file ("c:\\foo.cs"). 我有“Type”代表我的类(公共类Foo),从那里,我想检索其父文件的路径(“c:\\ foo.cs”)。

Thanks 谢谢

Edit: The purpose of this would be to show some classes' name to the user in a ListView (populated programmatically) for example and when he double clicks on one of the ListViewItem, it opens the .cs file that contains the class in question. 编辑:这样做的目的是在ListView(以编程方式填充)中向用户显示某些类的名称,例如,当他双击其中一个ListViewItem时,它会打开包含相关类的.cs文件。

If you bundle your source code, then sure. 如果您捆绑源代码,那么肯定。 In which case, you already know where it is. 在这种情况下,您已经知道它在哪里。

However, source code is NOT included by default with a compiled program, and as such you cannot retrieve it at run time. 但是,默认情况下,源代码不包含在已编译的程序中,因此您无法在运行时检索它。

Perhaps if you expand on what you're trying to do, we may be able to help? 也许如果你扩展你正在尝试做的事情,我们可能会提供帮助吗?

Yes you can using Source Server although you would have to distribute indexed symbol files (PDBs) with your program. 是的,您可以使用源服务器,但您必须使用您的程序分发索引符号文件(PDB)。

Setting up a Source Server implementation is non-trivial, to say the least. 至少可以说,设置源服务器实现并非易事。

The basics of the mechanism are that all source-code for your application is published to a web server, and your PDB files are re-written with information that ties type information to locations of the source code (ie URIs). 该机制的基础是将应用程序的所有源代码发布到Web服务器,并使用将类型信息绑定到源代码位置(即URI)的信息重写您的PDB文件。 In theory you could then use this information at runtime to request the appropriate source file from the web server. 理论上,您可以在运行时使用此信息从Web服务器请求相应的源文件。

It is possible, but there is no 'magic' way that I know of. 这是可能的,但我所知道的并没有“神奇”的方式。 You'll have to add a method to every type you want to support this. 您必须为要支持此类型的每种类型添加方法。 Also it will only work if the .pdb file is available. 它也只有在.pdb文件可用时才有效。 _GetSourceFileName returns null without it. _GetSourceFileName没有它就返回null。

using System;
using System.Reflection;

class Program
{
    static void Main(string[] args)
    {
        DumpFilenameForType(typeof(A));
        DumpFilenameForType(typeof(B));

        if (System.Diagnostics.Debugger.IsAttached)
        {
            System.Console.Write("Press any key to continue . . . ");
            System.Console.ReadKey();
        }
    }

    static void DumpFilenameForType(Type t)
    {
        MethodInfo mi = t.GetMethod("_GetSourceFileName", BindingFlags.Static | BindingFlags.NonPublic);
        if (mi != null)
            Console.WriteLine("Type '{0}' is located in '{1}'", t.FullName, mi.Invoke(null, null));
        else
            Console.WriteLine("Type '{0}' does not provide method _GetSourceFileName", t.FullName);
    }
}

// Some other file //其他一些文件

public class A
{
    static string _GetSourceFileName()
    {
        return new System.Diagnostics.StackTrace(true).GetFrame(0).GetFileName();
    }
}

// Yet another other file //还有另一个文件

public class B
{
    static string _GetSourceFileName()
    {
        return new System.Diagnostics.StackTrace(true).GetFrame(0).GetFileName();
    }
}

As there is no reference to the sourcecode in the assembly itself, this would only work if you have access to the debugging databases (.pdb) as well. 由于在程序集本身中没有对源代码的引用,因此只有在您可以访问调试数据库(.pdb)时才能使用它。 Even then, it still will be very hard to get to this information. 即便如此,仍然很难获得这些信息。

When you compile your C# program, the source code gets translated into CIL and placed inside an assembly. 编译C#程序时,源代码将转换为CIL并放置在程序集中。 The original raw source file is not copied over, so the Type object can not retrieve it. 原始源文件未复制,因此Type对象无法检索它。

In order to access the source code, you will need to include it in your build process and load the file without using the Type object. 要访问源代码,您需要将其包含在构建过​​程中并加载文件而不使用Type对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM