简体   繁体   English

在C#中如何防止类被修改

[英]In c# how to prevent a class from being modified

Let's say I have an interface: 假设我有一个接口:

interface IPerson
{
   int Age { get; set; }
   string Name { get; set; }
   bool Alive { get; set; }
}

and a Class: 和一个类:

public class Person : IPerson
{
   public int Age { get; set; }
   public string Name { get; set; }
}

That would not compile since Person does not implement the Alive Property. 由于Person没有实现Alive Property,因此无法编译。

What I would like to know is if there is a way to have the same behaviour, if Person adds an extra property that is not found in its interface. 我想知道的是,如果Person添加了在其界面中找不到的额外属性,是否有一种方法可以具有相同的行为。

interface IPerson
{
   int Age { get; set; }
   string Name { get; set; }
}

and a Class: 和一个类:

public class Person : IPerson
{
   public int Age { get; set; }
   public string Name { get; set; }
   public bool Alive { get; set; }  <---- This should prevent it from compiling as well.
}

I would want it to not compile as well, or at the very least give me a compile warning. 我希望它也不能编译,或者至少给我一个编译警告。

No. Interfaces define what members an object must implement. 否。接口定义对象必须实现的成员。 They cannot define members that an object can't implement. 他们无法定义对象无法实现的成员。 You could potentially use your own custom, or third party code analysis tools, to identify cases like this, but there is nothing in the language itself that would support it. 您可能会使用自己的自定义或第三方代码分析工具来识别类似的情况,但是语言本身没有任何东西可以支持它。

No it is not possible. 不,这是不可能的。 Are you not trying to define a class itself here? 您不是要在此处定义class本身吗? Why do you really need an interface? 为什么真正需要一个界面?

However, what you can achieve is make calls to your interface and not your class by doing 但是,您可以实现的是通过执行接口而不是类来进行调用

 IPerson person = new Person();

this way you limit the person object to access only the methods defined in your interface. 这样,您可以限制人员对象仅访问界面中定义的方法。

There is a way to do this, but not within the object and its a bit silly to use in construction of the object. 有一种方法可以做到这一点,但是不能在对象内部进行,并且在对象的构造中使用它有点愚蠢。 Using Reflection , you can check the type of an instance of your object and iterate through its properties, throwing an exception if the count or names of properties do not match your desired instance. 使用Reflection ,您可以检查对象实例的type并遍历对象的属性,如果属性的计数或名称与所需实例不匹配,则会引发异常。 Please note that if you just check against the interface, implementing objects will pass, so you would check against the desired concrete type. 请注意,如果仅检查接口,则实现对象将通过,因此将检查所需的具体类型。 I am on my phone or I'd add some sample code, will try to return to this later. 我在手机上,或者要添加一些示例代码,稍后将尝试返回此代码。

There is a way to do this, but not within the object and its a bit silly to use in construction of the object. 有一种方法可以做到这一点,但是不能在对象内部进行,并且在对象的构造中使用它有点愚蠢。 Using Reflection , you can check the type of an instance of your object and iterate through its properties, throwing an exception if the count or names of properties do not match your desired instance. 使用Reflection ,您可以检查对象实例的type并遍历对象的属性,如果属性的计数或名称与所需实例不匹配,则会引发异常。 Please note that if you just check against the interface, implementing objects will pass, so you would check against the desired concrete type. 请注意,如果仅检查接口,则实现对象将通过,因此将检查所需的具体类型。 I'm on my phone so no sample code. 我在手机上,所以没有示例代码。 Will try to return to this later. 稍后将尝试返回到此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM