/// <summary> /// Load a code unit directly from the specified source file (without a <see cref="Project"/> or <see cref="Solution"/>). /// </summary> /// <param name="codeFragment">A fragment of code as a string.</param> /// <param name="name">An optional name for the code fragment.</param> /// <param name="loadOptions">Determines various optional processing.</param> /// <returns>The resulting <see cref="CodeUnit"/> object.</returns> /// <remarks> /// The code fragment is parsed. /// Note that any compiler directive symbols will be undefined. To work around this issue, create /// a dummy parent project to hold one or more <see cref="CodeUnit"/>s before parsing. /// </remarks> public static CodeUnit LoadFragment(string codeFragment, string name, LoadOptions loadOptions) { CodeUnit codeUnit = null; try { // Create a dummy project for the fragment Project project = new Project(name + Project.CSharpProjectFileExtension, null); // Create and parse the code fragment, and log statistics codeUnit = new CodeUnit(name, codeFragment, project); codeUnit.ParseLog(loadOptions, null); } catch (Exception ex) { Log.Exception(ex, "loading fragment"); } return(codeUnit); }
/// <summary> /// Load a <see cref="CodeUnit"/> directly from the specified source file (without a <see cref="Project"/> or <see cref="Solution"/>). /// </summary> /// <param name="fileName">The source (".cs") file.</param> /// <param name="loadOptions">Determines various optional processing.</param> /// <param name="statusCallback">Status callback for monitoring progress.</param> /// <returns>The resulting CodeUnit object.</returns> /// <remarks> /// Loading a code unit directly goes through the following steps: /// - Parse the code file. /// Note that any compiler directive symbols will be undefined. To work around this issue, create /// a dummy parent project to hold one or more <see cref="CodeUnit"/>s before parsing. /// </remarks> public static CodeUnit Load(string fileName, LoadOptions loadOptions, Action <LoadStatus, CodeObject> statusCallback) { CodeUnit codeUnit = null; try { // Handle a relative path to the file if (!Path.IsPathRooted(fileName)) { fileName = FileUtil.CombineAndNormalizePath(Environment.CurrentDirectory, fileName); } // Abort if the file doesn't exist - otherwise, the Parse() method will end up returning a valid but empty object // with an error message attached (which is done so errors can appear inside a loaded Solution tree). if (!File.Exists(fileName)) { Log.WriteLine("ERROR: File '" + fileName + "' does not exist."); return(null); } // Create a dummy project for the file, since it is being loaded directly Project project = new Project(Path.ChangeExtension(fileName, Project.CSharpProjectFileExtension), null); if (statusCallback != null) { statusCallback(LoadStatus.ObjectCreated, project); } // Create and parse the code unit, and log statistics codeUnit = new CodeUnit(fileName, project); codeUnit.ParseLog(loadOptions, statusCallback); } catch (Exception ex) { Log.Exception(ex, "loading file"); } return(codeUnit); }