@manhng

Welcome to my blog!

Get All Assemblies

January 19, 2021 20:46

Get All Assemblies (edit)

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace ConsoleApp1
{
internal class Program
{
private static string filePath = string.Empty;
private static string fileName = string.Empty;
private static string fileNameWithNoExtension = string.Empty;

private static void Main(string[] args)
{
List<Assembly> allAssemblies = new List<Assembly>();

StringBuilder sbType = new StringBuilder();
StringBuilder sbMethod = new StringBuilder();

string referenceIncludeLine = string.Empty;

string folderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

bool searchOnlyInterface = false; // Search Class, Abstract
//bool searchOnlyInterface = true; // Search Interface

Type[] types;

string[] arrayDllStartsWith = new[] { "Abc", "Def" };
string[] arrayDllEndsWith = new[] { ".cs" };
string[] arrayContainWords = new[] { "Export", "Pdf" };
string[] arrayNotContainWords = new[] { "Get_", "Set_" };

foreach (string dll in Directory.GetFiles(folderPath, "*.dll"))
{
try
{
if (!string.IsNullOrWhiteSpace(dll))
{
filePath = dll;
fileNameWithNoExtension = Path.GetFileNameWithoutExtension(dll);

bool anyItem = false;
foreach (var item in arrayDllStartsWith)
{
if (fileNameWithNoExtension.StartsWith(item))
{
anyItem = true;
break;
}
}

// Type name not contain ".cs"
bool neverItem = false;
foreach (var item in arrayDllEndsWith)
{
if (!fileNameWithNoExtension.EndsWith(item))
{
neverItem = true;
break;
}
}
if (anyItem && neverItem)
{
var assembly = Assembly.LoadFile(dll);
if (assembly != null)
{
allAssemblies.Add(assembly);
}
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}

Console.WriteLine("Count Assemblies: {0}", allAssemblies.Count);

foreach (var assembly in allAssemblies)
{
try
{
if (assembly != null)
{
if (searchOnlyInterface)
{
types = assembly.GetTypes().Where(type => type.IsClass && type.IsAbstract).ToArray();
}
else
{
types = assembly.GetTypes().Where(type => type.IsInterface).ToArray();
}

foreach (Type type in types)
{
try
{
if (type != null && !string.IsNullOrWhiteSpace(type.Name))
{
string typeName = type.Name.Replace("`1", "").Replace("`2", "").Replace("`3", "");

// Type name contain "Pdf"
// Type name contain "Export"
bool trueContain = false;
foreach (var word in arrayContainWords)
{
if (typeName.Contains(word))
{
trueContain = true;
break;
}
}
if (trueContain)
{
Console.WriteLine(typeName);
sbType.AppendLine(typeName);

var methods = type.GetMethods();
try
{
foreach (var method in methods)
{
string methodName = method.Name.Replace("`1", "").Replace("`2", "").Replace("`3", "");

// Method name contain "Pdf"
// Method name contain "Export"
bool trueContain2 = false;
foreach (var word in arrayContainWords)
{
if (methodName.Contains(word))
{
trueContain2 = true;
break;
}
}

// Method name not contain Get_
// Method name not contain Set_
bool notContain = false;
foreach (var word in arrayNotContainWords)
{
if (!methodName.Contains(word))
{
notContain = true;
break;
}
}

if (trueContain2 && !notContain)
{
Console.WriteLine(methodName);
sbMethod.AppendLine(methodName);
}
}
}
catch (Exception e3)
{
Debug.WriteLine(e3.ToString());
}
}
}
}
catch (Exception e2)
{
Debug.WriteLine(e2.ToString());
}
}
}
}
catch (Exception e1)
{
Debug.WriteLine(e1.ToString());
}
}

File.WriteAllText(Path.Combine(folderPath, "Types.txt"), sbType.ToString(), Encoding.UTF8);
File.WriteAllText(Path.Combine(folderPath, $"Types_{DateTime.Now.ToString("yyyyMMddHHmmm")}.txt"), sbType.ToString(), Encoding.UTF8);

File.WriteAllText(Path.Combine(folderPath, "Methods.txt"), sbMethod.ToString(), Encoding.UTF8);
File.WriteAllText(Path.Combine(folderPath, $"Methods_{DateTime.Now.ToString("yyyyMMddHHmmm")}.txt"), sbMethod.ToString(), Encoding.UTF8);

Console.WriteLine("---------- ----------");
Console.WriteLine("Done");
Console.ReadLine();
}
}
}

Using Reflection

using System;
using System.IO;
using System.Text;

namespace ConsoleApp1
{
internal class Program
{
private static string ReferenceInclude = "<Reference Include=\"{0}\"><HintPath>{1}</HintPath></Reference>";
private static string fileNameWithOutExtension = string.Empty;
private static string folderPath = @"C:\temp\";

private static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();

if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}

foreach (string dll in Directory.GetFiles(folderPath, "*.dll"))
{
if (!string.IsNullOrWhiteSpace(dll))
{
fileNameWithOutExtension = Path.GetFileNameWithoutExtension(dll);

if (fileNameWithOutExtension.StartsWith("MyNamespace") && !fileNameWithOutExtension.EndsWith(".cs"))
{
sb.AppendLine(string.Format(ReferenceInclude, fileNameWithOutExtension, dll));
}
}
}

File.WriteAllText(Path.Combine(folderPath, "DEF.txt"), sb.ToString(), Encoding.UTF8);
File.WriteAllText(Path.Combine(folderPath, $"DEF_{DateTime.Now.ToString("yyyyMMddHHmmm")}.txt"), sb.ToString(), Encoding.UTF8);
}
}
}

Loading .NET Assemblies out of Seperate Folders - Rick Strahl's Web Log (west-wind.com)

c# - how to load all assemblies from within your /bin directory - Stack Overflow

using System;
using System.Reflection;
using System.Security.Policy;

class ADGetAssemblies
{
public static void Main()
{
AppDomain currentDomain = AppDomain.CurrentDomain;

//Provide the current application domain evidence for the assembly.
Evidence asEvidence = currentDomain.Evidence;
//Load the assembly from the application directory using a simple name.

//Create an assembly called CustomLibrary to run this sample.
currentDomain.Load("CustomLibrary",asEvidence);

//Make an array for the list of assemblies.
Assembly[] assems = currentDomain.GetAssemblies();

//List the assemblies in the current application domain.
Console.WriteLine("List of assemblies loaded in current appdomain:");
foreach (Assembly assem in assems)
Console.WriteLine(assem.ToString());
}
}

Categories

Recent posts