mscorlib(4.0.0.0) API with additions
AssemblyName.cs
3 using System.IO;
7 using System.Security;
9 using System.Security.Util;
10 
11 namespace System.Reflection
12 {
14  [Serializable]
15  [ClassInterface(ClassInterfaceType.None)]
16  [ComDefaultInterface(typeof(_AssemblyName))]
17  [ComVisible(true)]
18  [__DynamicallyInvokable]
20  {
21  private string _Name;
22 
23  private byte[] _PublicKey;
24 
25  private byte[] _PublicKeyToken;
26 
27  private CultureInfo _CultureInfo;
28 
29  private string _CodeBase;
30 
31  private Version _Version;
32 
33  private StrongNameKeyPair _StrongNameKeyPair;
34 
35  private SerializationInfo m_siInfo;
36 
37  private byte[] _HashForControl;
38 
39  private AssemblyHashAlgorithm _HashAlgorithm;
40 
41  private AssemblyHashAlgorithm _HashAlgorithmForControl;
42 
43  private AssemblyVersionCompatibility _VersionCompatibility;
44 
45  private AssemblyNameFlags _Flags;
46 
49  [__DynamicallyInvokable]
50  public string Name
51  {
52  [__DynamicallyInvokable]
53  get
54  {
55  return _Name;
56  }
57  [__DynamicallyInvokable]
58  set
59  {
60  _Name = value;
61  }
62  }
63 
66  [__DynamicallyInvokable]
67  public Version Version
68  {
69  [__DynamicallyInvokable]
70  get
71  {
72  return _Version;
73  }
74  [__DynamicallyInvokable]
75  set
76  {
77  _Version = value;
78  }
79  }
80 
83  [__DynamicallyInvokable]
85  {
86  [__DynamicallyInvokable]
87  get
88  {
89  return _CultureInfo;
90  }
91  [__DynamicallyInvokable]
92  set
93  {
94  _CultureInfo = value;
95  }
96  }
97 
100  [__DynamicallyInvokable]
101  public string CultureName
102  {
103  [__DynamicallyInvokable]
104  get
105  {
106  if (_CultureInfo != null)
107  {
108  return _CultureInfo.Name;
109  }
110  return null;
111  }
112  [__DynamicallyInvokable]
113  set
114  {
115  _CultureInfo = ((value == null) ? null : new CultureInfo(value));
116  }
117  }
118 
121  public string CodeBase
122  {
123  get
124  {
125  return _CodeBase;
126  }
127  set
128  {
129  _CodeBase = value;
130  }
131  }
132 
135  public string EscapedCodeBase
136  {
137  [SecuritySafeCritical]
138  get
139  {
140  if (_CodeBase == null)
141  {
142  return null;
143  }
144  return EscapeCodeBase(_CodeBase);
145  }
146  }
147 
150  [__DynamicallyInvokable]
152  {
153  [__DynamicallyInvokable]
154  get
155  {
156  int num = (int)(_Flags & (AssemblyNameFlags)112) >> 4;
157  if (num > 5)
158  {
159  num = 0;
160  }
161  return (ProcessorArchitecture)num;
162  }
163  [__DynamicallyInvokable]
164  set
165  {
166  int num = (int)(value & (ProcessorArchitecture)7);
167  if (num <= 5)
168  {
169  _Flags = (AssemblyNameFlags)((long)_Flags & 4294967055L);
170  _Flags |= (AssemblyNameFlags)(num << 4);
171  }
172  }
173  }
174 
178  [ComVisible(false)]
179  [__DynamicallyInvokable]
181  {
182  [__DynamicallyInvokable]
183  get
184  {
185  int num = (int)(_Flags & (AssemblyNameFlags)3584) >> 9;
186  if (num > 1)
187  {
188  num = 0;
189  }
190  return (AssemblyContentType)num;
191  }
192  [__DynamicallyInvokable]
193  set
194  {
195  int num = (int)(value & (AssemblyContentType)7);
196  if (num <= 1)
197  {
198  _Flags = (AssemblyNameFlags)((long)_Flags & 4294963711L);
199  _Flags |= (AssemblyNameFlags)(num << 9);
200  }
201  }
202  }
203 
206  [__DynamicallyInvokable]
207  public AssemblyNameFlags Flags
208  {
209  [__DynamicallyInvokable]
210  get
211  {
212  return _Flags & (AssemblyNameFlags)(-3825);
213  }
214  [__DynamicallyInvokable]
215  set
216  {
217  _Flags &= (AssemblyNameFlags)3824;
218  _Flags |= (value & (AssemblyNameFlags)(-3825));
219  }
220  }
221 
225  {
226  get
227  {
228  return _HashAlgorithm;
229  }
230  set
231  {
232  _HashAlgorithm = value;
233  }
234  }
235 
239  {
240  get
241  {
242  return _VersionCompatibility;
243  }
244  set
245  {
246  _VersionCompatibility = value;
247  }
248  }
249 
253  {
254  get
255  {
256  return _StrongNameKeyPair;
257  }
258  set
259  {
260  _StrongNameKeyPair = value;
261  }
262  }
263 
266  [__DynamicallyInvokable]
267  public string FullName
268  {
269  [SecuritySafeCritical]
270  [__DynamicallyInvokable]
271  get
272  {
273  string text = nToString();
274  if (CompatibilitySwitches.IsAppEarlierThanWindowsPhone8 && string.IsNullOrEmpty(text))
275  {
276  return base.ToString();
277  }
278  return text;
279  }
280  }
281 
283  [__DynamicallyInvokable]
284  public AssemblyName()
285  {
286  _HashAlgorithm = AssemblyHashAlgorithm.None;
287  _VersionCompatibility = AssemblyVersionCompatibility.SameMachine;
288  _Flags = AssemblyNameFlags.None;
289  }
290 
293  public object Clone()
294  {
295  AssemblyName assemblyName = new AssemblyName();
296  assemblyName.Init(_Name, _PublicKey, _PublicKeyToken, _Version, _CultureInfo, _HashAlgorithm, _VersionCompatibility, _CodeBase, _Flags, _StrongNameKeyPair);
297  assemblyName._HashForControl = _HashForControl;
298  assemblyName._HashAlgorithmForControl = _HashAlgorithmForControl;
299  return assemblyName;
300  }
301 
315  [SecuritySafeCritical]
316  public static AssemblyName GetAssemblyName(string assemblyFile)
317  {
318  if (assemblyFile == null)
319  {
320  throw new ArgumentNullException("assemblyFile");
321  }
322  string fullPathInternal = Path.GetFullPathInternal(assemblyFile);
323  new FileIOPermission(FileIOPermissionAccess.PathDiscovery, fullPathInternal).Demand();
324  return nGetFileInformation(fullPathInternal);
325  }
326 
327  internal void SetHashControl(byte[] hash, AssemblyHashAlgorithm hashAlgorithm)
328  {
329  _HashForControl = hash;
330  _HashAlgorithmForControl = hashAlgorithm;
331  }
332 
336  [__DynamicallyInvokable]
337  public byte[] GetPublicKey()
338  {
339  return _PublicKey;
340  }
341 
344  [__DynamicallyInvokable]
345  public void SetPublicKey(byte[] publicKey)
346  {
347  _PublicKey = publicKey;
348  if (publicKey == null)
349  {
350  _Flags &= ~AssemblyNameFlags.PublicKey;
351  }
352  else
353  {
354  _Flags |= AssemblyNameFlags.PublicKey;
355  }
356  }
357 
360  [SecuritySafeCritical]
361  [__DynamicallyInvokable]
362  public byte[] GetPublicKeyToken()
363  {
364  if (_PublicKeyToken == null)
365  {
366  _PublicKeyToken = nGetPublicKeyToken();
367  }
368  return _PublicKeyToken;
369  }
370 
373  [__DynamicallyInvokable]
374  public void SetPublicKeyToken(byte[] publicKeyToken)
375  {
376  _PublicKeyToken = publicKeyToken;
377  }
378 
381  [__DynamicallyInvokable]
382  public override string ToString()
383  {
384  string fullName = FullName;
385  if (fullName == null)
386  {
387  return base.ToString();
388  }
389  return fullName;
390  }
391 
397  [SecurityCritical]
399  {
400  if (info == null)
401  {
402  throw new ArgumentNullException("info");
403  }
404  info.AddValue("_Name", _Name);
405  info.AddValue("_PublicKey", _PublicKey, typeof(byte[]));
406  info.AddValue("_PublicKeyToken", _PublicKeyToken, typeof(byte[]));
407  info.AddValue("_CultureInfo", (_CultureInfo == null) ? (-1) : _CultureInfo.LCID);
408  info.AddValue("_CodeBase", _CodeBase);
409  info.AddValue("_Version", _Version);
410  info.AddValue("_HashAlgorithm", _HashAlgorithm, typeof(AssemblyHashAlgorithm));
411  info.AddValue("_HashAlgorithmForControl", _HashAlgorithmForControl, typeof(AssemblyHashAlgorithm));
412  info.AddValue("_StrongNameKeyPair", _StrongNameKeyPair, typeof(StrongNameKeyPair));
413  info.AddValue("_VersionCompatibility", _VersionCompatibility, typeof(AssemblyVersionCompatibility));
414  info.AddValue("_Flags", _Flags, typeof(AssemblyNameFlags));
415  info.AddValue("_HashForControl", _HashForControl, typeof(byte[]));
416  }
417 
420  public void OnDeserialization(object sender)
421  {
422  if (m_siInfo != null)
423  {
424  _Name = m_siInfo.GetString("_Name");
425  _PublicKey = (byte[])m_siInfo.GetValue("_PublicKey", typeof(byte[]));
426  _PublicKeyToken = (byte[])m_siInfo.GetValue("_PublicKeyToken", typeof(byte[]));
427  int @int = m_siInfo.GetInt32("_CultureInfo");
428  if (@int != -1)
429  {
430  _CultureInfo = new CultureInfo(@int);
431  }
432  _CodeBase = m_siInfo.GetString("_CodeBase");
433  _Version = (Version)m_siInfo.GetValue("_Version", typeof(Version));
434  _HashAlgorithm = (AssemblyHashAlgorithm)m_siInfo.GetValue("_HashAlgorithm", typeof(AssemblyHashAlgorithm));
435  _StrongNameKeyPair = (StrongNameKeyPair)m_siInfo.GetValue("_StrongNameKeyPair", typeof(StrongNameKeyPair));
436  _VersionCompatibility = (AssemblyVersionCompatibility)m_siInfo.GetValue("_VersionCompatibility", typeof(AssemblyVersionCompatibility));
437  _Flags = (AssemblyNameFlags)m_siInfo.GetValue("_Flags", typeof(AssemblyNameFlags));
438  try
439  {
440  _HashAlgorithmForControl = (AssemblyHashAlgorithm)m_siInfo.GetValue("_HashAlgorithmForControl", typeof(AssemblyHashAlgorithm));
441  _HashForControl = (byte[])m_siInfo.GetValue("_HashForControl", typeof(byte[]));
442  }
443  catch (SerializationException)
444  {
445  _HashAlgorithmForControl = AssemblyHashAlgorithm.None;
446  _HashForControl = null;
447  }
448  m_siInfo = null;
449  }
450  }
451 
452  internal AssemblyName(SerializationInfo info, StreamingContext context)
453  {
454  m_siInfo = info;
455  }
456 
465  [SecuritySafeCritical]
466  [__DynamicallyInvokable]
467  public AssemblyName(string assemblyName)
468  {
469  if (assemblyName == null)
470  {
471  throw new ArgumentNullException("assemblyName");
472  }
473  if (assemblyName.Length == 0 || assemblyName[0] == '\0')
474  {
475  throw new ArgumentException(Environment.GetResourceString("Format_StringZeroLength"));
476  }
477  _Name = assemblyName;
478  nInit();
479  }
480 
486  [SecuritySafeCritical]
487  public static bool ReferenceMatchesDefinition(AssemblyName reference, AssemblyName definition)
488  {
489  if (reference == definition)
490  {
491  return true;
492  }
493  return ReferenceMatchesDefinitionInternal(reference, definition, parse: true);
494  }
495 
496  [MethodImpl(MethodImplOptions.InternalCall)]
497  [SecuritySafeCritical]
498  internal static extern bool ReferenceMatchesDefinitionInternal(AssemblyName reference, AssemblyName definition, bool parse);
499 
500  [MethodImpl(MethodImplOptions.InternalCall)]
501  [SecurityCritical]
502  internal extern void nInit(out RuntimeAssembly assembly, bool forIntrospection, bool raiseResolveEvent);
503 
504  [SecurityCritical]
505  internal void nInit()
506  {
507  RuntimeAssembly assembly = null;
508  nInit(out assembly, forIntrospection: false, raiseResolveEvent: false);
509  }
510 
511  internal void SetProcArchIndex(PortableExecutableKinds pek, ImageFileMachine ifm)
512  {
513  ProcessorArchitecture = CalculateProcArchIndex(pek, ifm, _Flags);
514  }
515 
516  internal static ProcessorArchitecture CalculateProcArchIndex(PortableExecutableKinds pek, ImageFileMachine ifm, AssemblyNameFlags flags)
517  {
518  if ((flags & (AssemblyNameFlags)240) == (AssemblyNameFlags)112)
519  {
520  return ProcessorArchitecture.None;
521  }
522  if ((pek & PortableExecutableKinds.PE32Plus) == PortableExecutableKinds.PE32Plus)
523  {
524  switch (ifm)
525  {
526  case ImageFileMachine.IA64:
527  return ProcessorArchitecture.IA64;
528  case ImageFileMachine.AMD64:
529  return ProcessorArchitecture.Amd64;
530  case ImageFileMachine.I386:
531  if ((pek & PortableExecutableKinds.ILOnly) == PortableExecutableKinds.ILOnly)
532  {
533  return ProcessorArchitecture.MSIL;
534  }
535  break;
536  }
537  }
538  else
539  {
540  switch (ifm)
541  {
542  case ImageFileMachine.I386:
543  if ((pek & PortableExecutableKinds.Required32Bit) == PortableExecutableKinds.Required32Bit)
544  {
545  return ProcessorArchitecture.X86;
546  }
547  if ((pek & PortableExecutableKinds.ILOnly) == PortableExecutableKinds.ILOnly)
548  {
549  return ProcessorArchitecture.MSIL;
550  }
551  return ProcessorArchitecture.X86;
552  case ImageFileMachine.ARM:
553  return ProcessorArchitecture.Arm;
554  }
555  }
556  return ProcessorArchitecture.None;
557  }
558 
559  internal void Init(string name, byte[] publicKey, byte[] publicKeyToken, Version version, CultureInfo cultureInfo, AssemblyHashAlgorithm hashAlgorithm, AssemblyVersionCompatibility versionCompatibility, string codeBase, AssemblyNameFlags flags, StrongNameKeyPair keyPair)
560  {
561  _Name = name;
562  if (publicKey != null)
563  {
564  _PublicKey = new byte[publicKey.Length];
565  Array.Copy(publicKey, _PublicKey, publicKey.Length);
566  }
567  if (publicKeyToken != null)
568  {
569  _PublicKeyToken = new byte[publicKeyToken.Length];
570  Array.Copy(publicKeyToken, _PublicKeyToken, publicKeyToken.Length);
571  }
572  if (version != null)
573  {
574  _Version = (Version)version.Clone();
575  }
576  _CultureInfo = cultureInfo;
577  _HashAlgorithm = hashAlgorithm;
578  _VersionCompatibility = versionCompatibility;
579  _CodeBase = codeBase;
580  _Flags = flags;
581  _StrongNameKeyPair = keyPair;
582  }
583 
587  void _AssemblyName.GetTypeInfoCount(out uint pcTInfo)
588  {
589  throw new NotImplementedException();
590  }
591 
597  void _AssemblyName.GetTypeInfo(uint iTInfo, uint lcid, IntPtr ppTInfo)
598  {
599  throw new NotImplementedException();
600  }
601 
609  void _AssemblyName.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
610  {
611  throw new NotImplementedException();
612  }
613 
624  void _AssemblyName.Invoke(uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
625  {
626  throw new NotImplementedException();
627  }
628 
629  internal string GetNameWithPublicKey()
630  {
631  byte[] publicKey = GetPublicKey();
632  return Name + ", PublicKey=" + Hex.EncodeHexString(publicKey);
633  }
634 
635  [MethodImpl(MethodImplOptions.InternalCall)]
636  [SecurityCritical]
637  internal static extern AssemblyName nGetFileInformation(string s);
638 
639  [MethodImpl(MethodImplOptions.InternalCall)]
640  [SecurityCritical]
641  private extern string nToString();
642 
643  [MethodImpl(MethodImplOptions.InternalCall)]
644  [SecurityCritical]
645  private extern byte[] nGetPublicKeyToken();
646 
647  [MethodImpl(MethodImplOptions.InternalCall)]
648  [SecurityCritical]
649  internal static extern string EscapeCodeBase(string codeBase);
650  }
651 }
AssemblyContentType
Provides information about the type of code contained in an assembly.
object Clone()
Makes a copy of this T:System.Reflection.AssemblyName object.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
FileIOPermissionAccess
Specifies the type of file access requested.
string EscapedCodeBase
Gets the URI, including escape characters, that represents the codebase.
AssemblyName()
Initializes a new instance of the T:System.Reflection.AssemblyName class.
string? CultureName
Gets or sets the name of the culture associated with the assembly.
AssemblyHashAlgorithm HashAlgorithm
Gets or sets the hash algorithm used by the assembly manifest.
Indicates that a class is to be notified when deserialization of the entire object graph has been com...
Encapsulates access to a public or private key pair used to sign strong name assemblies.
void Invoke(uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
Provides access to properties and methods exposed by an object.
Definition: __Canon.cs:3
AssemblyContentType ContentType
Gets or sets a value that indicates what type of content the assembly contains.
ImageFileMachine
Identifies the platform targeted by an executable.
AssemblyName(string assemblyName)
Initializes a new instance of the T:System.Reflection.AssemblyName class with the specified display n...
virtual string Name
Gets the culture name in the format languagecode2-country/regioncode2.
Definition: CultureInfo.cs:321
AssemblyNameFlags
Provides information about an T:System.Reflection.Assembly reference.
Describes the source and destination of a given serialized stream, and provides an additional caller-...
Version Version
Gets or sets the major, minor, build, and revision numbers of the assembly.
Definition: AssemblyName.cs:68
AssemblyNameFlags Flags
Gets or sets the attributes of the assembly.
AssemblyVersionCompatibility VersionCompatibility
Gets or sets the information related to the assembly's compatibility with other assemblies.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
string Name
Gets or sets the simple name of the assembly. This is usually, but not necessarily,...
Definition: AssemblyName.cs:51
static bool ReferenceMatchesDefinition(AssemblyName reference, AssemblyName definition)
Returns a value indicating whether two assembly names are the same. The comparison is based on the si...
static AssemblyName GetAssemblyName(string assemblyFile)
Gets the T:System.Reflection.AssemblyName for a given file.
void SetPublicKey(byte[] publicKey)
Sets the public key identifying the assembly.
CultureInfo CultureInfo
Gets or sets the culture supported by the assembly.
Definition: AssemblyName.cs:85
Supports cloning, which creates a new instance of a class with the same value as an existing instance...
Definition: ICloneable.cs:7
void GetTypeInfo(uint iTInfo, uint lcid, IntPtr ppTInfo)
Retrieves the type information for an object, which can be used to get the type information for an in...
override string ToString()
Returns the full name of the assembly, also known as the display name.
Represents the version number of an assembly, operating system, or the common language runtime....
Definition: Version.cs:11
The exception thrown when an error occurs during serialization or deserialization.
MethodImplOptions
Defines the details of how a method is implemented.
StrongNameKeyPair KeyPair
Gets or sets the public and private cryptographic key pair that is used to create a strong name signa...
AssemblyVersionCompatibility
Defines the different types of assembly version compatibility. This feature is not available in versi...
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
void GetObjectData(SerializationInfo info, StreamingContext context)
Gets serialization information with all the data needed to recreate an instance of this AssemblyName.
byte [] GetPublicKey()
Gets the public key of the assembly.
string CodeBase
Gets or sets the location of the assembly as a URL.
Describes an assembly's unique identity in full.
Definition: AssemblyName.cs:19
The exception that is thrown when one of the arguments provided to a method is not valid.
void Demand()
Forces a T:System.Security.SecurityException at run time if all callers higher in the call stack have...
void GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
Maps a set of names to a corresponding set of dispatch identifiers.
Allows an object to control its own serialization and deserialization.
Definition: ISerializable.cs:8
string GetString(string name)
Retrieves a T:System.String value from the T:System.Runtime.Serialization.SerializationInfo store.
PortableExecutableKinds
Identifies the nature of the code in an executable file.
object GetValue(string name, Type type)
Retrieves a value from the T:System.Runtime.Serialization.SerializationInfo store.
Specifies that the class can be serialized.
virtual int LCID
Gets the culture identifier for the current T:System.Globalization.CultureInfo.
Definition: CultureInfo.cs:304
ClassInterfaceType
Identifies the type of class interface that is generated for a class.
int GetInt32(string name)
Retrieves a 32-bit signed integer value from the T:System.Runtime.Serialization.SerializationInfo sto...
Specifies that the parameter is an input parameter.
object Clone()
Returns a new T:System.Version object whose value is the same as the current T:System....
Definition: Version.cs:277
void OnDeserialization(object sender)
Implements the T:System.Runtime.Serialization.ISerializable interface and is called back by the deser...
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
Controls the ability to access files and folders. This class cannot be inherited.
ProcessorArchitecture ProcessorArchitecture
Gets or sets a value that identifies the processor and bits-per-word of the platform targeted by an e...
ProcessorArchitecture
Identifies the processor and bits-per-word of the platform targeted by an executable.
Exposes the T:System.Reflection.AssemblyName class to unmanaged code.
void GetTypeInfoCount(out uint pcTInfo)
Retrieves the number of type information interfaces that an object provides (either 0 or 1).
Performs operations on T:System.String instances that contain file or directory path information....
Definition: Path.cs:13
byte [] GetPublicKeyToken()
Gets the public key token, which is the last 8 bytes of the SHA-1 hash of the public key under which ...
string FullName
Gets the full name of the assembly, also known as the display name.
AssemblyHashAlgorithm
Specifies all the hash algorithms used for hashing files and for generating the strong name.
void SetPublicKeyToken(byte[] publicKeyToken)
Sets the public key token, which is the last 8 bytes of the SHA-1 hash of the public key under which ...