Reputation: 383
Suppose I have a internal class "B" which is derived from a abstract class,example
abstract class A
{
private int _abc;
public int abc
{
get{return _int;}
set{_abc=value;}
}
}
internal class B:A
{
}
I need abc in different assembly,can i access it ??? please help me.
Upvotes: 0
Views: 3613
Reputation: 5833
If you need to safe it internal by the some reason, you have a few ways to access from the external code
1) You can use InternalVisibleToAttribute to grant access to access internals to friendly assemblies
[assembly:InternalVisibleTo("Test.dll")]
2) You can use Reflection to access a member of an object
object obj = <instance of your internal type>;
var value = obj.GetType().GetProperty("abc").GetValue(obj);
3) You can introduce a public interface which will provide a contract to access abc property
public interface IAbcAccessor
{
int abc {get; set;}
}
internal abstract class A
: IAbcAccessor
{
private int _abc;
public int abc
{
get{return _int;}
set{_abc=value;}
}
int IAbcAccessor.abc
{
get{return abc;}
set{abc = value;}
}
}
External code can access property by the using Interface
var accessor = (IAbcAccessor)<instance of your internal type>;
var value = accessor.abc;
Upvotes: 1
Reputation: 51917
You cannot create an instance of B or declare anything to be of type B outside of the assembly that B is declared in. E.g
B b = whatever
Or
A b = new B()
Is not allowed outside of the assembly that B is declared in.
However if you have a public method
public class C
{
public static A MakeAB()
{
return new B();
}
}
in the assembly that B is declared in, then use can say
A a = C.MakeABe()
in any assembly, as the type B does not need to be known by the caller of C.MakeAB()
Upvotes: 0