Check SA Version

From RedGateWiki
Jump to: navigation, search

Back to SmartAssembly SDK

You can use Mono's assembly metadata reader to retrieve the PoweredBy CustomAttribute that gets added to every assembly that has been processed using SmartAssembly.Note that you will need to download the Mono Cecil library and reference it in your project -- Microsoft's Reflection does not give you the ability to access the attribute value.


C#

  1. using System;
  2. using Mono.Cecil; // Reference Mono.Cecil 0.6.0.0
  3.  
  4. namespace EnumCustomAttributes
  5. {
  6.                 class Program
  7.                 {
  8.                                 static void Main(string[] args)
  9.                                 {
  10.                                                 AssemblyDefinition asm = AssemblyFactory.GetAssembly(@"C:\My.exe");
  11.                                                 foreach (CustomAttribute attr in asm.CustomAttributes)
  12.                                                 {
  13.                                                                 if (attr.Constructor.DeclaringType.FullName == "SmartAssembly.Attributes.PoweredByAttribute")
  14.                                                                 {
  15.                                                                                 Console.WriteLine(attr.ConstructorParameters[0]);
  16.                                                                 }
  17.                                                 }
  18.                                 }
  19.                 }
  20. }

VB

  1. Imports Mono.Cecil
  2. ' Reference Mono.Cecil 0.6.0.0
  3. Namespace EnumCustomAttributes
  4.   Class Program
  5.     Private Shared Sub Main(args As String())
  6.       Dim asm As AssemblyDefinition = AssemblyFactory.GetAssembly("C:\My.exe")
  7.       For Each attr As CustomAttribute In asm.CustomAttributes
  8.         If attr.Constructor.DeclaringType.FullName = "SmartAssembly.Attributes.PoweredByAttribute" Then
  9.           Console.WriteLine(attr.ConstructorParameters(0))
  10.         End If
  11.       Next
  12.     End Sub
  13.   End Class
  14. End Namespace
Personal tools