Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get class of an internal static class in another assembly?

I have a class C in Assembly A like this:

internal class C
{
  internal static string About_Name {
      get { return "text"; }
  ...
}

I have about 20 such static properties. Is there a way, in an outside assembly, without using friend assembly attribute (.Net reflection only), get class C so I can invoke any of the static string properties like this:

Class C = <some .Net reflection code>;
string expected = C.About_Name;

If that is not possible, a .Net reflection code to get the string property value directly will suffice, but not ideal.

like image 680
Echiban Avatar asked Apr 19 '10 06:04

Echiban


1 Answers

Try this...
Edit: I did not think about just using the type instead of object instance when it was a static property.
Removed var obj = Activator.CreateInstance(type); and use type in prop.GetValue instead of obj.

namespace ClassLibrary1
{
    internal class Class1
    {
        internal static string Test { get { return "test"; } }
    }
    public class Class2
    {

    }
}

var ass = Assembly.GetAssembly(typeof(Class2));
var type = ass.GetType("ClassLibrary1.Class1");
var prop = type.GetProperty("Test", BindingFlags.Static 
    | BindingFlags.NonPublic);
var s = (string)prop.GetValue(type, null);
like image 179
Jens Granlund Avatar answered Sep 29 '22 07:09

Jens Granlund