vendredi 11 septembre 2015

C# Getting the file path of the .cs file where a type was declared

I'm working on a procedural asset generation system and I want it to be able to detect if the source file of a particular asset has changed so that it only has to regenerate assets that will actually be different.

Some googling has told me that there is no way to get the source file of a type using simple reflection, so I'm trying to come up with a workaround. Here's what I'm doing:

  1. Get a list of all .cs files in the project directory using Directory.GetFiles.
  2. Compile each file into its own assembly using CSharpCodeProvider.CompileAssemblyFromFile.
  3. If compiledAssembly.GetType(targetType.FullName) exists, then that is the source file of targetType.

The problem is that step 2 is giving compile errors without any sort of description:

  • (0,0) : error :
  • (0,0) : error : at (wrapper managed-to-native) System.Reflection.Assembly:GetTypes (bool)
  • (0,0) : error : at (wrapper managed-to-native) System.Reflection.Assembly:GetTypes (bool)
  • etc.

I think that this may be because the current assembly or app domain or whatever already contains the exact type it's trying to compile, but that's just a guess. Does anyone know what might be causing those errors or how I can avoid them?





Aucun commentaire:

Enregistrer un commentaire