禁止扩展方法(Forbid extension methods)
我正在发布一个公共类作为我的C#框架的一部分,以便用户能够在我的应用程序中注册。 我不希望用户扩展我的课程,因此我将其标记为
sealed
。 现在我有一些用户报告他们在该类中看到的方法多于我提供的方法。 经过一些调查后,似乎这些用户已经定义了(通过intellisense)显示的扩展方法,就像它们是在我的公共密封类中定义的一样。问题有没有办法可以禁止用户在公共密封类上定义扩展方法?
I am publishing a public class as part of my C# framework for users to be able to register within my application. I don't want users to extend my class so I marked it
sealed
. Now I have some users reporting that they see more methods in that class than the ones I provided. After some investigation, it appeared that these users have defined extension methods that show (by intellisense) just like if they were defined in my public sealed class.Question Is there a way I can forbid users to define extension methods on my public sealed class?
原文:https://stackoverflow.com/questions/11172500
最满意答案
我认为在你的情况下,你需要确保函数
calculateAge
能够calculateAge
传递对象的年龄。 例如,为了计算对象的年龄(看起来它是一个动物:)),函数calculateAge
知道无论对象的类型如何,它都有一个名为getAge
的方法来完成这项工作。 因此,您需要在calculateAge
执行的所有操作都是调用传递对象的getAge
。你可以这样做,例如:
interface Animal { public function getAge(); } // The same thing for the class Dog class Cat implements Animal { public function getAge() { // code to calculate age } ... } public function calculateAge(Animal $animal) { echo $animal->getAge(); } $dog = new Dog(); calculateAge($dog); $cat = new Cat(); calculateAge($cat);
I think in your case you need at to make sure your function
calculateAge
is able to calcualte the age of the passed object. For instance, to calculate the age of the object (it seems that it's an animal :) ), your functioncalculateAge
knows that whatever the type of the object, it has a method calledgetAge
that does the job. So all what you need to do incalculateAge
is to callgetAge
of the passed object.You can do this for instance:
interface Animal { public function getAge(); } // The same thing for the class Dog class Cat implements Animal { public function getAge() { // code to calculate age } ... } public function calculateAge(Animal $animal) { echo $animal->getAge(); } $dog = new Dog(); calculateAge($dog); $cat = new Cat(); calculateAge($cat);
相关问答
更多-
我如何创建一个函数,在C中使用一个未知类型的参数?(How can I create a function that takes a parameter of unknown type in C?)[2023-10-30]
使用void *param ,指向任何...在glib中通常用作gpointer的指针 use void *param, a pointer to anything ... commonly used in glib as gpointer -
我认为在你的情况下,你需要确保函数calculateAge能够calculateAge传递对象的年龄。 例如,为了计算对象的年龄(看起来它是一个动物:)),函数calculateAge知道无论对象的类型如何,它都有一个名为getAge的方法来完成这项工作。 因此,您需要在calculateAge执行的所有操作都是调用传递对象的getAge 。 你可以这样做,例如: interface Animal { public function getAge(); } // The same thing fo ...
-
删除大括号{ 。 你在方法后给了两个大括号 /** * @path("/getList") * @param(string, query,'string') * @return(string,'text/plain') */ public function getList($ID) { .... } Got the resolution. Instead of saying @param(string, query,'string') use @pa ...
-
魔鬼的细节如下: 函数定义类似于insert_chem_pair但我调用insert_chempair 看来DST影响了我。 The devil is in details: Function is defined like this insert_chem_pair but i call insert_chempair It seems DST affected me.
-
我敢打赌你正在寻找模板 。 D有一个伟大的语言参考 ,是免费的在线。 I bet you are looking for templates. D has a great language reference that is free online.
-
你也不知道函数参数,并用模板参数解决了这个问题(好吧,参数包,因为有零个或更多)。 只要做同样的返回类型! template
class Sample { public: Sample(Args... args, std::function fnct) : _inputBuff(std::forward_as_tuple(args...)) { } ... -
如果您想像上面所做的那样实例化类型,则可以将类名作为字符串传递。 function check($class_str) { $x = new $class_str(); if($x instanceof Test) { print_r("the object is from the type of the passed variable! :)"); } } check('Test'); If you want to instantiate the type lik ...
-
首先更改声明: typedef void (Person::*FnSetStr)(const std::string& s); typedef void (Person::*FnSetInt)(const int& i); 然后改变通话: setMem("Name", person, &Person::name ); // (1) setMem("Age", person, &Person::age ); // (2) 在VS 2010中以警告级别4构建清洁。 First change th ...
-
因为你正在检查!= NULL我想你有一个指针数组。 使用指针数组,您的代码可以工作,因为您知道指针的大小,即使它是一个void指针。 如果我的假设是正确的,你可以这样编码: int nb_elems(void** array) { void** end = array; while(*end != NULL) { end++; } return end - array; } 但此代码仅适用于指针指针或指针数组。 用法: int** iptrs = (int* ...
-
功能必须知道他们的类型。 否则,为它们生成代码将非常困难,并且链接器肯定无法处理它们。 话虽这么说,就像C ++一样,D有模板,所以你可以声明函数模板,以便在编译使用它的代码时生成和编译函数本身。 因此,如果您想要一个带有两个相同类型参数但可以使用各种类型的函数的函数,那么您将需要使用函数模板。 例如 void foo(T)(T a, T b) { ... } 或者如果需要多种类型,你可以做类似的事情 void foo(T, U)(T a, U b) { ... } 在任何一种情况下,当 ...