Find a class its .cs file in project

2020-07-27 04:54发布

How can I find the .cs file which contains a specified class in a project by code?

I imagine it could look like this:

string pathToFoo = ClassFileFinder.FindClassPath(typeof(Foo));

标签: file-io c#
3条回答
淡お忘
2楼-- · 2020-07-27 05:15

Are you trying to modify the file at runtime? That's not a very good idea and a huge security concern. You could always try to use System.Reflector to see the contents of the class at runtime. Also, if you're wanting to add a function to a class that is in a library, you can use an extension method.

查看更多
姐就是有狂的资本
3楼-- · 2020-07-27 05:17

It does not make any sense what you are trying to do. You wont have any .cs file in compiled executable at the end.

Moreover, you may have different class filenames than class identifers declared in class definitions so I doubt simple file search will also not guarantee finding file in source code. So IMO its not possible.

查看更多
【Aperson】
4楼-- · 2020-07-27 05:27

I have written and tested the class based on some ideas I had and in found in the comments. It works reasonably fast and I haven't found any problems with it so far.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;

public static class ClassFileFinder
{
static List<string> classFiles;

public static ClassFileDetails FindClassFile(System.Type t)
{
    return FindClassFile(t.Name);
}

public static ClassFileDetails FindClassFile(string className)
{
    ClassFileDetails details = DatabaseLink.GetClassFileDetails(className);
    if (details == null)
    {
        //Lookup class name in file names
        classFiles = new List<string>();
        FindAllScriptFiles(Application.dataPath);
        Debug.Log(classFiles.Count);
        for (int i = 0; i < classFiles.Count; i++)
        {
            if (classFiles[i].Contains(className))
            {
                details = new ClassFileDetails(className, classFiles[i],    File.GetLastAccessTimeUtc(classFiles[i]));
            }
        }
        //Lookup class name in the class file text 
        if (details == null)
        {
            for (int i = 0; i < classFiles.Count; i++)
            {
                string codeFile = File.ReadAllText(classFiles[i]);
                if (codeFile.Contains("class " + className))
                {
                    details = new ClassFileDetails(className, classFiles[i], File.GetLastAccessTimeUtc(classFiles[i]));
                }
            }
        }
        if (details == null)
        {
            Debug.LogWarning("Failed to lookup class file for class " + className);
        }
        return details;
    }
    else
    {
        return details;
    }
}

     static void FindAllScriptFiles(string startDir)
    {
   try
   {
       foreach (string file in Directory.GetFiles(startDir))
       {
           if (file.Contains(".cs") || file.Contains(".js"))
               classFiles.Add(file);
       }
       foreach (string dir in Directory.GetDirectories(startDir))
       {
           FindAllScriptFiles(dir);
       }
   }
   catch (System.Exception ex)
   {
       Debug.Log(ex.Message);
   }
   }
}

public class ClassFileDetails
{
public int OID { get; set; }
public string className { get; set; }
[Sqo.Attributes.UniqueConstraint]
public string path { get; set; }
public System.DateTime updateTime { get; set; }

internal ClassFileDetails()
{ }
internal ClassFileDetails(string setClassName, string setPath, System.DateTime setUpdateTime)
{
    className = setClassName;
    path = setPath;
    updateTime = setUpdateTime;

    DatabaseLink.StoreClassFileDetails(this);
}

}

查看更多
登录 后发表回答