예제 #1
0
 internal CompilerMessageEventArgs(CompilerMessage message, FileLineNumber fileLineNumber)
     : this(
         message,
         fileLineNumber == null ? null : fileLineNumber.SourceFile,
         fileLineNumber == null ? 0 : fileLineNumber.LineNumber,
         fileLineNumber == null ? 0 : fileLineNumber.LinePosition)
 {
 }
예제 #2
0
 public CompilerMessageEventArgs(CompilerMessage message, string fileName, int lineNumber, int linePosition, int?linePositionEnd = null)
 {
     this.Message         = message;
     this.FileName        = fileName;
     this.LineNumber      = lineNumber;
     this.LinePosition    = linePosition;
     this.LinePositionEnd = linePositionEnd;
 }
예제 #3
0
 // NOTE: 'id' is ignored!
 public void OnError(object sender, int id, string fileName, string format, params object[] details)
 {
     if (this.MessageDelegate != null)
     {
         CompilerMessageEventArgs e = new CompilerMessageEventArgs(CompilerMessage.InternalError(String.Format(CultureInfo.InvariantCulture, format, details)), fileName, 0, 0);
         this.MessageDelegate(sender, e);
     }
 }
예제 #4
0
 internal CompilerMessageEventArgs(CompilerMessage message, string sourceFile, Range range)
     : this(
         message,
         sourceFile == null ? null : sourceFile,
         range == null ? 0 : range.Start.Line,
         range == null ? 0 : range.Start.Column,
         range == null ? 0 : range.End.Column)
 {
 }
        private static Assembly ExtensionLoadFrom(string assemblyName)
        {
            Assembly extensionAssembly = null;

            try
            {
                extensionAssembly = Assembly.LoadFrom(assemblyName);
            }
            catch (Exception e)
            {
                throw new CompilerException(new CompilerMessageEventArgs(CompilerMessage.InvalidExtension(assemblyName, e.Message), null, 0, 0), e);
            }

            return(extensionAssembly);
        }
예제 #6
0
 public CompilerMessageEventArgs(CompilerMessage message, PackageItem item)
     : this(message, item.LineNumber)
 {
 }
        /// <summary>
        /// Loads a CompilerExtension from a type description string.
        /// </summary>
        /// <param name="extension">The extension type description string.</param>
        /// <returns>The loaded CompilerExtension.</returns>
        /// <remarks>
        /// <paramref name="extension"/> can be in several different forms:
        /// <list type="number">
        /// <item><term>AssemblyQualifiedName (TopNamespace.SubNameSpace.ContainingClass+NestedClass, MyAssembly, Version=1.3.0.0, Culture=neutral, PublicKeyToken=b17a5c561934e089)</term></item>
        /// <item><term>AssemblyName (MyAssembly, Version=1.3.0.0, Culture=neutral, PublicKeyToken=b17a5c561934e089)</term></item>
        /// <item><term>Absolute path to an assembly (C:\MyExtensions\ExtensionAssembly.dll)</term></item>
        /// <item><term>Filename of an assembly in the application directory (ExtensionAssembly.dll)</term></item>
        /// <item><term>Relative path to an assembly (..\..\MyExtensions\ExtensionAssembly.dll)</term></item>
        /// </list>
        /// To specify a particular class to use, prefix the fully qualified class name to the assembly and separate them with a comma.
        /// For example: "TopNamespace.SubNameSpace.ContainingClass+NestedClass, C:\MyExtensions\ExtensionAssembly.dll"
        /// </remarks>
        public static CompilerExtension Load(string extension)
        {
            Type   extensionType = null;
            int    commaIndex    = extension.IndexOf(',');
            string className     = String.Empty;
            string assemblyName  = extension;

            if (0 <= commaIndex)
            {
                className    = extension.Substring(0, commaIndex);
                assemblyName = (extension.Length <= commaIndex + 1 ? String.Empty : extension.Substring(commaIndex + 1));
            }

            className    = className.Trim();
            assemblyName = assemblyName.Trim();

            if (null == extensionType && 0 < assemblyName.Length)
            {
                Assembly extensionAssembly;

                // case 3: Absolute path to an assembly
                if (Path.IsPathRooted(assemblyName))
                {
                    extensionAssembly = ExtensionLoadFrom(assemblyName);
                }
                else
                {
                    try
                    {
                        // case 2: AssemblyName
                        extensionAssembly = Assembly.Load(assemblyName);
                    }
                    catch (IOException e)
                    {
                        if (e is FileLoadException || e is FileNotFoundException)
                        {
                            try
                            {
                                // case 4: Filename of an assembly in the application directory
                                extensionAssembly = Assembly.Load(Path.GetFileNameWithoutExtension(assemblyName));
                            }
                            catch (IOException innerE)
                            {
                                if (innerE is FileLoadException || innerE is FileNotFoundException)
                                {
                                    // case 5: Relative path to an assembly

                                    // we want to use Assembly.Load when we can because it has some benefits over Assembly.LoadFrom
                                    // (see the documentation for Assembly.LoadFrom). However, it may fail when the path is a relative
                                    // path, so we should try Assembly.LoadFrom one last time. We could have detected a directory
                                    // separator character and used Assembly.LoadFrom directly, but dealing with path canonicalization
                                    // issues is something we don't want to deal with if we don't have to.
                                    extensionAssembly = ExtensionLoadFrom(assemblyName);
                                }
                                else
                                {
                                    throw new CompilerException(new CompilerMessageEventArgs(CompilerMessage.InvalidExtension(assemblyName, innerE.Message), null, 0, 0), innerE);
                                }
                            }
                        }
                        else
                        {
                            throw new CompilerException(new CompilerMessageEventArgs(CompilerMessage.InvalidExtension(assemblyName, e.Message), null, 0, 0), e);
                        }
                    }
                }

                if (0 < className.Length)
                {
                    try
                    {
                        // case 1: AssemblyQualifiedName
                        extensionType = extensionAssembly.GetType(className, true, true);
                    }
                    catch (Exception e)
                    {
                        throw new CompilerException(new CompilerMessageEventArgs(CompilerMessage.InvalidExtensionClassName(assemblyName, className, e.Message), null, 0, 0), e);
                    }
                }
                else
                {
                    // if no class name was specified, then let's hope the assembly defined a default CompilerExtension
                    DefaultCompilerExtensionAttribute extensionAttribute = (DefaultCompilerExtensionAttribute)Attribute.GetCustomAttribute(extensionAssembly, typeof(DefaultCompilerExtensionAttribute));

                    if (null != extensionAttribute)
                    {
                        extensionType = extensionAttribute.ExtensionType;
                    }
                    else
                    {
                        throw new CompilerException(new CompilerMessageEventArgs(CompilerMessage.ExpectedDefaultCompilerExtensionAttribute(assemblyName, typeof(DefaultCompilerExtensionAttribute)), null, 0, 0));
                    }
                }
            }

            if (extensionType.IsSubclassOf(typeof(CompilerExtension)))
            {
                return(Activator.CreateInstance(extensionType) as CompilerExtension);
            }
            else
            {
                throw new CompilerException(new CompilerMessageEventArgs(CompilerMessage.InvalidExtensionType(extension, extensionType, typeof(CompilerExtension)), null, 0, 0));
            }
        }