Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Friend Assembly in C#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 503
    Comment on it

    Friend Assembly in C#

     

    • Access modifiers such as internal and private when used with types or type members defined in files in an assembly are accessed only in the files defined within same assembly.
    • An assembly can only access public members of another assembly. Private and internal types and members remain inaccessible.
    • A friend assembly allows internal members or internal type of assembly to be accessed in another assembly.
    • When an internal type defined in assembly 1 is to be used in assembly 2 then "InternalsVisibleToAttribute" attribute is used in Assembly 1 and [assembly: InternalsVisibleToAttribute("Name_of_Assembly")] line is to be included in Assembly 1.
    • With Friend assembly, only internal members of another assembly are accessible, private types and private members still cannot be accessed.                                                                               

     

    Example to Demonstrate Friend Assembly in C#:

     

    Steps to create Friend Assembly in C#:

    • Create a new project solution with two project types. One is a class library project named ClassLibrary other project is a console application named Testing.
    • Define a public and an internal member in classLibrary project and build it.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.CompilerServices;
    
    [assembly: InternalsVisibleToAttribute("Testing")]
    namespace ClassLibrary
    {
        public class Class1
        {
    
        }
        internal class Class2
        {
    
        }
    }
    • Add ClassLibrary project reference in Testing project.
    • To access internal member add [assembly: InternalsVisibleToAttribute("Testing")] line in ClassLibrary project. ClassLibrary is said to be a friend assembly. 
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ClassLibrary;
    
    namespace Testing
    {
        class Program
        {
            static void Main(string[] args)
            {
                Class1 class1 = new Class1();
                Class2 class2 = new Class2();
            }
        }
    }
    • While trying to access public and internal members of ClassLibrary project in Testing project. An error "ClassLibrary.Class2 is inaccessible due to it's protection level" occurs if  "InternalsVisibleToAttribute" attribute  is not added in ClassLibrary project.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: