Accessing information in AssemblyInfo.cs using Reflection on the website

I created a DLL that will collect information from AssemblyInfo.cs. In the class constructor, I use Reflection to get the topmost application that works.

public class AppInfo()
{
    public AppInfo()
    {
        System.Reflection.Assembly assembly =
            System.Reflection.Assembly.GetEntryAssembly();
        if (assembly == null)
            assembly = System.Reflection.Assembly.GetCallingAssembly();


        //code to gather needed information
    }
}

How is it supposed to be used if I call it from any DLL in this MyApp application, which allows me to say that the name will always be "MyApp". Getting this information is not a problem, and it works great in Windows Services and Windows Applications. My question is: How to get the assembly of the topmost website?

, Global.asax.cs, AssemblyInfo.cs - App_Code . Option AssemblyInfo.cs

<compiler
language="c#;cs;csharp"
extension=".cs"
compilerOptions="C:\Sandbox\MyWebSite\AssemblyInfo.cs"
type="Microsoft.CSharp.CSharpCodeProvider,System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4">

, AssemblyInfo.cs - System.Reflection.Assembly.GetExecutingAssembly(). AppInfo, Assembly , DLL, MyWebSite, new AppInfo(), DLL -.

, - -, , , , -. , AssemblyInfo.cs -, , , DLL ?

EDIT: -, Windows Windows

+3
4

, , Assembly.GetEntryAssembly() null -, Assembly.GetCallingAssembly() , , - . , "Entry Assembly", , . System.Web .., IIS -, , , . , , , , ...

var trace = new StackTrace();
Assembly entryAssembly = null;
foreach (var frame in trace.GetFrames())
{
   var assembly = frame.GetMethod().DeclaringType.Assembly;
   //check if the assembly is one you own & therefore could be your logical
   //"entry assembly". You could do this by checking the prefix of the
   //Assembly Name if you use some standardised naming convention, or perhaps
   //looking at the AssemblyCompanyAttribute, etc
   if ("assembly is one of mine")
   {
      entryAssembly = assembly;
   }
}

, - ... , .

+2

AppDomains , bin *.dll AppDomain. "-" , , , - .

. System.Web.Hosting.ProcessHost, System.Web.ApplicationHost System.Web.Hosting.ApplicationManger System.Web.Hosting.Environment, System.AppDomain .

AppDomain , , , AppDomain, .

, - .

MAY , ​​ "

String binDir = Server.MapPath("/bin/");
Response.Write(String.Format("Bin dir:{0}<br/><br/>",binDir));

foreach (string file in Directory.GetFiles(binDir, "*.dll"))
{
    Response.Write(String.Format("File:{0}<br/>", file));
    try
    {
        Assembly assembly = Assembly.LoadFile(file);
        object[] attrinutes = assembly.GetCustomAttributes(true);
        foreach (var o in attrinutes)
        {
            //AssemblyCompanyAttribute is set in the AssemblyInfo.cs
            if (o is AssemblyCompanyAttribute)
            {
                Response.Write(String.Format("Company Attribute: Company = {0}<br/>", ((AssemblyCompanyAttribute)o).Company));
                continue;   
            }
            Response.Write(String.Format("Attribute: {0}<br/>", o));
        }
    }
    catch(Exception ex)
    {
        Response.Write(String.Format("Exception Reading File: {0} Exception: {1}",file,ex));
    }
}

, Master ( App_Code) , . , ? - - ?

+1

Alconja, . GalobalAssemblyCache, . , , DLL, .

The following class helps get a Type object if only the assembly name and type name are known.

public static class AssemblyHandler {

    private static Dictionary<String, Assembly> Assemblies;

    public static Assembly GetAssembly(String Name) {

        if (Assemblies == null) {
            Assemblies = new Dictionary<string, Assembly>();
            var mainAsm = Assembly.GetEntryAssembly();

            if (mainAsm == null) {
                var trace = new StackTrace();
                foreach (var frame in trace.GetFrames()) {
                    var assembly = frame.GetMethod().DeclaringType.Assembly;
                    if (assembly.GlobalAssemblyCache) {
                        break;
                    }
                    mainAsm = assembly;
                }
            }

            Assemblies.Add(mainAsm.FullName, mainAsm);
            ScanReferencedAssemblies(mainAsm);
        }

        if (!Assemblies.ContainsKey(Name)) {
            return null;
        }
        return Assemblies[Name];
    }

    private static void ScanReferencedAssemblies(Assembly Asm) {

        foreach (var refAsmName in Asm.GetReferencedAssemblies()) {
            Assembly a = Assembly.Load(refAsmName);
            if (a.GlobalAssemblyCache) {
                continue;
            }
            if (!Assemblies.ContainsKey(a.FullName)) {
                Assemblies.Add(a.FullName, a);
                ScanReferencedAssemblies(a);
            }
        }
    }

    public static Type GetType(string AssemblyName, string TypeName) {

        return GetAssembly(AssemblyName).GetType(TypeName);
    }
}
0
source

Source: https://habr.com/ru/post/1706559/


All Articles