when we use protected internal any data member of any class then we know that data member can use in same assembly but i did not know that protected internal can be used from other assembly. from here i come to know Have you ever seen design with reasonable usage of protected internal access modifier?
public class A
{
internal protected virtual void YoBusiness()
{
//do something
}
public void test() { }
}
class B
{ // not a derived class - just composites an instance of A
public B()
{
A a = new A();
a.YoBusiness(); // Thanks friend for the access!
}
}
class D : A
{ // derived across assemblies
internal protected override void YoBusiness()
{
// Hey thanks other guy, I can provide a new implementation.
}
}
class C : A
{ // derived across assemblies
public C()
{
YoBusiness();
}
protected override void YoBusiness()
{
// Hey thanks other guy, I can provide a new implementation.
}
}
i knew always that protected internal can be used in same assembly but today know and surprise that any class from other assembly also could override the method......how it is getting possible?
if i want that only the data member can be override or call from same assembly then what i need to do......please discuss. thanks
If you only want the method to be overridable or callable from the same assembly then just flag the method as internal
.
You can find more information on C# access modifiers here .
For an example of why you might use protected internal
lets assume we've got a base class called Job
which you derive from in order to add some functionality. The framework that runs the job will give you a Context
which has information on how the job should run. Only the framework can set this context, but the job should be able to access it. You could design your class like this:
abstract class Job
{
public abstract void Run()
protected internal Context RunContext{get; internal set}
}
Now, only your framework can set the context, as long as it's in the same assembly. Your derived classes can only get the context, assuming they're in a different assembly.
That's because protected internal
should be read as "protected or internal" (ie, visible to any class within the same assembly or any class derived from A
regardless of its location), and not as "protected and internal".
From MSDN :
The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.
i want that only the data member can be override or call from same assembly then what i need to do
Then just mark it as internal
and virtual
.
protected internal
does not mean what you (and many other developers including myself) think it does. It does not mean protected
and internal
, it means protected
or internal
. With this knowledge we know that classes that subclass the protected internal
member will have visibility for that member, a class in another assembly will not. The member is also visible anywhere in the assembly it was declared, regardless of whether or not the class accessing the member sub-classed the original class.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.