Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use internal class of another Assembly [duplicate]

I have a third party assembly and I would like to use its Internal class in my new C# project. Is it possible?

Any example would really be appreciated

like image 444
user1878422 Avatar asked Dec 06 '14 16:12

user1878422


People also ask

How do you call an internal method from another assembly?

internal: The type or member can be accessed by any code in the same assembly, but not from another assembly. You can not use internal classes of other assemblies, the point of using internal access modifier is to make it available just inside the assembly the class defined.

Can we create instance for an internal class?

An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance. To instantiate an inner class, you must first instantiate the outer class.

When to use internal in C#?

Internal, in C#, is a keyword used to declare the accessibility of a type or type member such that the access is limited to the assembly in which it is declared. An internal modifier is used to prevent the use of a public modifier, which allows access to other assemblies wherever necessary.

What is a friend assembly?

A friend assembly is an assembly that can access another assembly's internal (C#) or Friend (Visual Basic) types and members. If you identify an assembly as a friend assembly, you no longer have to mark types and members as public in order for them to be accessed by other assemblies.


1 Answers

internal: The type or member can be accessed by any code in the same assembly, but not from another assembly.

You can not use internal classes of other assemblies, the point of using internal access modifier is to make it available just inside the assembly the class defined.

if you have access to the assembly code and you can modify it you can make second assembly as a friend of your current assembly and mark the assembly with following attribute

[assembly: InternalsVisibleTo("name of assembly here")]

if not you can always use reflection but be aware that using reflection on a 3rd party assembly is dangerous because it is subject to change by the vendor. you can also decompile the whole assembly and use part of the code you want if it is possible.

Suppose you have this dll (mytest.dll say):

using System; 

namespace MyTest 
{ 
      internal class MyClass 
      {  
          internal void MyMethod() 
          {  
               Console.WriteLine("Hello from MyTest.MyClass!"); 
          } 
      } 
} 

and you want to create an instance of MyTest.MyClass and then call MyMethod() from another program using reflection. Here's how to do it:

using System; 
using System.Reflection;

namespace MyProgram 
{ 
    class MyProgram 
    { 
          static void Main() 
          { 
              Assembly assembly = Assembly.LoadFrom("mytest.dll");
              object mc = assembly.CreateInstance("MyTest.MyClass");
              Type t = mc.GetType(); 
              BindingFlags bf = BindingFlags.Instance |  BindingFlags.NonPublic;
              MethodInfo mi = t.GetMethod("MyMethod", bf); 
              mi.Invoke(mc, null); 
              Console.ReadKey(); 
         } 
    } 
}  
like image 69
Hamid Pourjam Avatar answered Oct 20 '22 05:10

Hamid Pourjam