using System;
using System.Reflection;
namespace Chernobyl.Reflection.Template.Xml
{
///
/// Holds the namespace and the assembly in one type.
///
public struct NamespaceAssembly
{
///
/// Initializes a new instance of the struct.
///
/// The string that identifies the namespace
/// represented by this .
/// The that this
/// represents.
public NamespaceAssembly(string @namespace, AssemblyName assembly)
: this()
{
Namespace = @namespace;
Assembly = assembly;
}
///
/// Initializes a new instance of the struct.
///
/// The string that contains the
/// namespace and assembly. It should be formatted like so:
/// clr-namespace:The.Namespace.Here; assembly=TheAssemblyNameHere
/// Thrown if the
/// does not contain the namespace
/// qualifier "clr-namespace:", a semicolon that separates the namespace
/// and assembly strings, or the assembly qualifier "assembly=".
public NamespaceAssembly(string namespaceAssembly) : this()
{
// Grab the namespace from the namespaceAssembly string.
// Grab the location of the namespace qualifier.
const string namespaceQualifier = "clr-namespace:";
int indexOfNamespaceQualifier = namespaceAssembly.IndexOf(namespaceQualifier);
if(indexOfNamespaceQualifier == -1)
throw new ArgumentException("The namespace assembly string is badly formed. " +
"The string must include the namespace qualifier \"" + namespaceQualifier +
"\" which specifies the namespace; this qualifier was not found. The value " +
"of the parameter was: \"" + namespaceAssembly + "\".", "namespaceAssembly");
// Grab the location of the semicolon separating the namespace and
// assembly.
int lastIndexOfNamespace = indexOfNamespaceQualifier + namespaceQualifier.Length;
int indexOfSemicolon = namespaceAssembly.IndexOf(';', lastIndexOfNamespace);
if(indexOfSemicolon == -1)
throw new ArgumentException("The namespace assembly string is badly formed. " +
"The string must include a semicolon separating the namespace and assembly; " +
"no semicolon was found. The value of the parameter was: \"" +
namespaceAssembly + "\".", "namespaceAssembly");
Namespace = namespaceAssembly.Substring(lastIndexOfNamespace, indexOfSemicolon - namespaceQualifier.Length);
// Grab the assembly name from the namespaceAssembly string.
const string assemblyQualifier = "assembly=";
int firstIndexOfAssembly = namespaceAssembly.IndexOf(assemblyQualifier, indexOfSemicolon);
if (indexOfNamespaceQualifier == -1)
throw new ArgumentException("The namespace assembly string is badly formed. " +
"The string must include the assembly qualifier \"" + assemblyQualifier +
"\" which specifies the assembly; this qualifier was not found. The value " +
"of the parameter was: \"" + namespaceAssembly + "\".", "namespaceAssembly");
int lastIndexOfAssembly = firstIndexOfAssembly + assemblyQualifier.Length;
string assemblyNameString = namespaceAssembly.Substring(lastIndexOfAssembly,
namespaceAssembly.Length - lastIndexOfAssembly);
Assembly = new AssemblyName(assemblyNameString);
}
///
/// The namespace.
///
public string Namespace { get; private set; }
///
/// The name of the assembly.
///
public AssemblyName Assembly { get; private set; }
}
}