[英]Dynamic method access modifier
I would like to restrict access to methods, depending on the type passed in. In my particular situation, I am developing a UDP "manager" if you will. 我想根据传入的类型来限制对方法的访问。在我的特殊情况下,如果您愿意,我正在开发UDP“管理器”。
I want my UDPManager
to be used for different things. 我希望我的
UDPManager
用于不同的事物。 For example, I might have 1 UDPManager
for the communications between client and server, and another UDPManager
to handle the communications between server and another server. 例如,我可能有1个
UDPManager
用于客户端与服务器之间的通信,而另一个UDPManager
用于处理服务器与另一台服务器之间的通信。
I have defined an enum
which specifies the type of UDPManager
. 我定义了一个
enum
,它指定UDPManager
的类型。 So for example, ManagerType.A = 1
and... ManagerType.B = 2
例如,
ManagerType.A = 1
,... ManagerType.B = 2
The UDPManager
has certain events that can be subscribed to and I do not want them available if these events are not relevant, given the type of UDPManager
. UDPManager
具有某些可以订阅的事件,并且在给定UDPManager
类型的情况下,如果这些事件不相关,我不希望它们可用。
Here is an example of a class 这是一个班级的例子
public class Something
{
public int SomethingsType { get; set; }
public void A() { }
public void B() { }
}
How can I make it so that if SomethingsType == MessageType.A
, then MessageType.B
is not available (ie it is private)? 我怎样才能做到,如果
SomethingsType == MessageType.A
,则MessageType.B
不可用(即它是私有的)?
For further clarity, if I type: 为了进一步说明,如果我键入:
Something something = new Something();
someting.SomethingsType = 1
I do not want something.B()
to be available. 我不希望有
something.B()
可用。
UPDATE 更新
I apologise for mentioning runtime
. 对于提及
runtime
我深表歉意。 What I mean is, I do not want said method ( B
) available if said SomethingsType
is A
. 我的意思是,如果所说的
SomethingsType
为A
,我不希望所说的方法( B
)可用。
Interfaces to the rescue: 救援接口:
public interface IUdpManagerA
{
void A();
}
public interface IUdpManagerB
{
void B();
}
public class UdpManager : IUdpManagerA, IUdpManagerB
{
public void A() { }
public void B() { }
}
public class UdpManagerFactory
{
private UdpManager Create() => new UdpManager();
public IUdpManagerA CreateOfA() => Create();
public IUdpManagerB CreateOfB() => Create();
}
UdpManagerFactory factory = new UdpManagerFactory();
IUdpManagerA a = factory.CreateOfA();
IUdpManagerB b = factory.CreateOfB();
Interfaces are a powerful tool to publish certain members while others can remain hidden. 界面是发布某些成员而其他成员可以隐藏的强大工具。
While you might say yeah, but you can always cast IUdpManagerA
to IUdpManagerB
and vice versa to gain access to hidden members , and my answer is **this isn't safe because there's no clue that IUdpManagerA
also implements IUdpManagerB
and vice versa. 虽然您可能会说是的,但是您始终可以将
IUdpManagerA
为IUdpManagerB
,反之亦然,以获取对隐藏成员的访问权限 ,我的回答是**这是不安全的,因为没有任何线索表明IUdpManagerA
也实现了IUdpManagerB
,反之亦然。
Oh, and I forgot to mention that you should throw away the ManagerType
enumeration, because with interfaces you can always check if a given instance is A or B : 哦,我忘了提起您应该丢弃
ManagerType
枚举,因为使用接口,您始终可以检查给定实例是A还是B :
object instance = factory.CreateA();
if(instance is IUdpManagerA)
{
}
if(instance is IUdpManagerB)
{
}
or using as
operator: 或
as
运算符:
object instance = factory.CreateA();
IUdpManagerA a = instance as IUdpManagerA;
IUdpManagerB b = instance as IUdpManagerB;
if(a != null)
{
}
else if(b != null)
{
}
This is an extreme simple version of a factory build method based of an enum: 这是基于枚举的工厂构建方法的极其简单的版本:
public enum ManagerType
{
A,B
}
public abstract class UDPManager
{
}
public class SomethingA : UDPManager
{
public void A()
{}
}
public class SomethingB : UDPManager
{
public void B()
{}
}
public class UdpManagerFactory
{
public UDPManager Build(ManagerType type)
{
if (type == ManagerType.A)
return new SomethingA();
if(type == ManagerType.B)
return new SomethingB();
throw new Exception("type not found");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.