/*
The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language
governing rights and limitations under the License.
The Original Code is RAIL(Runtime Assembly Instrumentation Library) Alpha Version.
The Initial Developer of the Original Code is University of Coimbra,
Computer Science Department, Dependable Systems Group. Copyright (C) University of Coimbra.
All Rights Reserved.
*/
namespace Rail.Reflect{
using System;
using System.Reflection;
using System.Globalization;
using System.Reflection.Emit;
/// <summary>
///
/// </summary>
public abstract class RAssemblyName
{
#region Fields
/// <summary>
///
/// </summary>
protected RVersion version;
/// <summary>
///
/// </summary>
protected string name;
/// <summary>
///
/// </summary>
protected CultureInfo cultureInfo;
/// <summary>
///
/// </summary>
protected AssemblyNameFlags flags;
/// <summary>
///
/// </summary>
protected byte[] publicKeyToken;
#endregion
#region Properties
/// <summary>
///
/// </summary>
public virtual RVersion Version
{
get
{
return this.version;
}
set
{
throw new NotSupportedException();
}
}
/// <summary>
///
/// </summary>
public virtual string Name
{
get
{
return this.name;
}
set
{
throw new NotSupportedException();
}
}
/// <summary>
///
/// </summary>
public virtual CultureInfo CultureInfo
{
get
{
return this.cultureInfo;
}
set
{
throw new NotSupportedException();
}
}
/// <summary>
/// Gets the full name of the assembly, also known as the display name.
/// </summary>
public virtual string FullName
{
get
{
string assemblyCulture = ", Culture=neutral";
//TODO: correct this FULLNAME Culture=???
string publicKeyToken = null;
if ( this.PublicKeyToken != null )
{
publicKeyToken = string.Format(", PublicKeyToken={0:x2}{1:x2}{2:x2}{3:x2}{4:x2}{5:x2}{6:x2}{7:x2}",
this.publicKeyToken[0], this.publicKeyToken[1], this.publicKeyToken[2], this.publicKeyToken[3],this.publicKeyToken[4], this.publicKeyToken[5], this.publicKeyToken[6], this.publicKeyToken[7]);
}
else
publicKeyToken=", PublicKeyToken=null";
string assemblyVersion = "";
if (this.Version!=null)
assemblyVersion= ", " + this.Version.ToString();
string fullName = string.Format("{0}{1}{2}{3}",
this.name,
assemblyVersion,
assemblyCulture,
publicKeyToken);
return fullName;
}
}
/// <summary>
/// Gets the flags for this assembly.
/// </summary>
public virtual AssemblyNameFlags Flags
{
get
{
return this.flags;
}
set
{
throw new NotSupportedException();
}
}
/// <summary>
///
/// </summary>
public virtual byte[] PublicKeyToken
{
get
{
return this.publicKeyToken;
}
set
{
throw new NotSupportedException();
}
}
#endregion
// TODO: These methods are only useful when the support for
// public keys is fully implemented
//
// public byte[] GetPublicKey() {
// //TODO: Return a copy of the byte array.
// }
//
// public void SetPublicKey(byte[] bArray) {
// //TODO: Save a copy of the byte array.
// }
}
/// <summary>
///
/// </summary>
public sealed class RAssemblyNameRef : RAssemblyName
{
/// <summary>
/// Creates a new instance representing the <code>AssemblyName</code>
/// object given.
/// </summary>
/// <param name="aName"></param>
public RAssemblyNameRef(AssemblyName aName)
{
this.cultureInfo = aName.CultureInfo;
this.flags = aName.Flags;
this.name = aName.Name;
this.publicKeyToken = aName.GetPublicKeyToken();
this.version = new RVersion(aName.Version.Major,
aName.Version.Minor,
aName.Version.Build,
aName.Version.Revision);
}
/// <summary>
/// To be used by the Rail API for internally creating instances.
/// </summary>
internal RAssemblyNameRef()
{
this.cultureInfo = CultureInfo.InvariantCulture;
this.name = null;
this.publicKeyToken = null;
this.version = new RVersion(0,0,0,0);
}
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <param name="vers"></param>
/// <param name="ci"></param>
/// <param name="flags"></param>
/// <param name="publickey"></param>
internal RAssemblyNameRef(string name,RVersion vers, CultureInfo ci, AssemblyNameFlags flags, byte [] publickey)
{
this.cultureInfo = CultureInfo.InvariantCulture;
this.name = name;
this.publicKeyToken = publickey;
this.version = vers;
this.flags = flags;
}
internal RAssemblyNameRef(string FullName)
{
string[] tokens = FullName.Split(new char[]{','});
//TODO: this is a workaround for culture
this.cultureInfo = CultureInfo.InvariantCulture;
int i = 0;
if (tokens!=null)
while (i<tokens.Length)
{
if (tokens[i].ToLower().IndexOf("version")>-1)
this.version = new RVersion(tokens[i]);
else if (tokens[i].ToLower().IndexOf("culture")>-1)
this.cultureInfo = CultureInfo.InvariantCulture;
else if (tokens[i].ToLower().IndexOf("publickey")>-1)
{
string val = tokens[i].Substring(tokens[i].IndexOf("=")+1,tokens[i].Length - (tokens[i].IndexOf("=")+1));
if (false == val.Equals("null"))
{
this.publicKeyToken = new byte[val.Length / 2/*SinceHexEncoded*/];
for(int j = 0;j < val.Length;j += 2)
this.publicKeyToken[j / 2] = Byte.Parse(val.Substring(j,2),System.Globalization.NumberStyles.HexNumber);
}
else
this.publicKeyToken = null;
}else
this.name = tokens[i];
++i;
}
}
}
/// <summary>
///
/// </summary>
public sealed class RAssemblyNameDef : RAssemblyName
{
#region Fields
#endregion
/// <summary>
/// Create instances of this class from the assembly that is being
/// manipulated.
/// </summary>
internal RAssemblyNameDef()
{
// Create a new version object
this.cultureInfo = CultureInfo.InvariantCulture;
this.name = null;
this.publicKeyToken = null;
this.version = new RVersion(0,0,0,0);
}
/// <summary>
/// Create a new RAssemblyNameDef
/// </summary>
/// <param name="name"></param>
public RAssemblyNameDef(String name)
{
// Create a new version object
this.cultureInfo = CultureInfo.InvariantCulture;
this.name = name;
this.publicKeyToken = null;
this.version = new RVersion(0,0,0,0);
}
/// <summary>
/// Create a new RAssemblyNameDef
/// </summary>
/// <param name="name"></param>
/// <param name="culture"></param>
/// <param name="major"></param>
/// <param name="minor"></param>
/// <param name="build"></param>
/// <param name="revision"></param>
/// <param name="publicKey"></param>
public RAssemblyNameDef(String name, CultureInfo culture , int major
,int minor, int build, int revision, byte [] publicKey)
{
// Create a new version object
this.cultureInfo = culture;
this.name = name;
this.publicKeyToken = publicKey;
this.version = new RVersion(major,minor,build,revision);
}
/// <summary>
///
/// </summary>
public override RVersion Version
{
set
{
this.version = value;
}
}
/// <summary>
///
/// </summary>
public override string Name
{
set
{
this.name = value;
}
}
/// <summary>
///
/// </summary>
public override CultureInfo CultureInfo
{
set
{
this.cultureInfo = value;
}
}
/// <summary>
/// Gets or sets the flags for this assembly.
/// </summary>
public override AssemblyNameFlags Flags
{
set
{
this.flags = value;
}
}
/// <summary>
///
/// </summary>
public override byte[] PublicKeyToken
{
set
{
this.publicKeyToken=value;
}
}
}
/// <summary>
///
/// </summary>
public sealed class RVersion
{
#region Fields
/// <summary>
///
/// </summary>
private int major;
/// <summary>
///
/// </summary>
private int minor;
/// <summary>
///
/// </summary>
private int build;
/// <summary>
///
/// </summary>
private int revision;
#endregion
#region Constructors
/// <summary>
///
/// </summary>
/// <param name="major"></param>
/// <param name="minor"></param>
/// <param name="build"></param>
/// <param name="revision"></param>
internal RVersion(int major, int minor, int build, int revision)
{
this.major = major;
this.minor = minor;
this.build = build;
this.revision = revision;
}
/// <summary>
///
/// </summary>
/// <param name="version"></param>
internal RVersion(string version)
{
string[] tokens = version.Split(new char[]{'.'});
if (tokens==null && tokens.Length==4)
throw new InvalidProgramException();
tokens[0] = tokens[0].Substring(tokens[0].IndexOf("=")+1,tokens[0].Length-(tokens[0].IndexOf("=")+1));
this.major = Int32.Parse(tokens[0]);
this.minor = Int32.Parse(tokens[1]);
this.build = Int32.Parse(tokens[2]);
this.revision = Int32.Parse(tokens[3]);
}
#endregion
#region Properties
/// <summary>
///
/// </summary>
public int Major
{
get
{
return this.major;
}
set
{
this.major = value;
}
}
/// <summary>
///
/// </summary>
public int Minor
{
get
{
return this.minor;
}
set
{
this.minor = value;
}
}
/// <summary>
///
/// </summary>
public int Build
{
get
{
return this.build;
}
set
{
this.build = value;
}
}
/// <summary>
///
/// </summary>
public int Revision
{
get
{
return this.revision;
}
set
{
this.revision = value;
}
}
#endregion
#region Overriden methods
/// <summary>
///
/// </summary>
/// <returns></returns>
public override string ToString()
{
string vers = string.Format("Version={0}.{1}.{2}.{3}",
this.major,
this.minor,
this.build,
this.revision);
return vers;
}
#endregion
}
}
|