C# Extension Method: Assembly - IsDefined

CSharp · Console

/Program.cs

// C# Extension Method
// Doc: https://csharp-extension.com/en/method/1002805/assembly-isdefined

// @nuget: Z.ExtensionMethods

using System;
using System.Reflection;

[assembly: AssemblyDescription("A sample description")]
namespace ExtensionMethods
{
	public class Program
	{
		public static void Main()
		{
			Type clsType = typeof(Program);

            Assembly assy = clsType.Assembly;

            String assyName = assy.GetName().Name;

			//C# Extension Method: Assembly - IsDefined
            bool isdef = assy.IsDefined(typeof(AssemblyDescriptionAttribute));

            if (isdef)
            {
                Console.WriteLine("The AssemblyDescription attribute is defined for assembly {0}.", assyName);

                AssemblyDescriptionAttribute adAttr = (AssemblyDescriptionAttribute)assy.GetCustomAttribute(typeof(AssemblyDescriptionAttribute));

                if (adAttr != null)
                    Console.WriteLine("The description is \"{0}\".", adAttr.Description);
                else
                    Console.WriteLine("The description could not be retrieved.");
            }
            else
                Console.WriteLine("The AssemblyDescription attribute is not defined for assembly {0}.", assyName);
		}
	}
}
;