可以将扩展方法应用于类吗?
例如,扩展DateTime以包含可以调用的Tomorrow()方法,如:
DateTime.Tomorrow();
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用
static DateTime Tomorrow(this Datetime value) { //... }
Run Code Online (Sandbox Code Playgroud)
要么
public static MyClass {
public static Tomorrow() { //... }
}
Run Code Online (Sandbox Code Playgroud)
对于类似的结果,但我如何扩展DateTime以便我可以调用DateTime.Tomorrow?
我正在寻找一个SQL查询,它输出数据库目录中所有用户定义函数的函数定义.
我找到了
SELECT OBJECT_DEFINITION (OBJECT_ID(N'dbo.UserFunctionName')) AS [Object Definition]
Run Code Online (Sandbox Code Playgroud)
和
SELECT ROUTINE_NAME FROM information_schema.routines WHERE routine_type = 'function'
Run Code Online (Sandbox Code Playgroud)
但我无法想到或找到将ROUTINE_NAME列表提供给OBJECT_ID的方法.
这里的目的是在数据库中用于数据库变更分析的用户定义函数定义的可搜索文本,如果像完整的SQL过程或有用的帮助程序更容易,我会这样做并发布它.
我有以下代码:
public static MyMethod()
{
...Do something
ProtectedMethod(param1, param2);
...Do something
}
protected static void ProtectedMethod(IEnumerable<string> param1, string param2, int param3 = 1)
{
... Do something
}
Run Code Online (Sandbox Code Playgroud)
注意可选的param3参数.
现在由于很多原因,我需要将MyMethod方法的代码提取到它自己的类中,但我不能用它提取ProtectedMethod,因为所有继承自这个的类都需要保持变化小而孤立.所以我想我可以在新类中使用与ProtectedMethod相同签名的Action <>委托.
问题是如果我像这样声明委托:
protected readonly Action<IEnumerable<string>, string, int> m_ProtectedMethod;
Run Code Online (Sandbox Code Playgroud)
提取的代码不喜欢它,因为它表示只使用两个参数调用该方法.
如果我像这样声明代表:
protected readonly Action<IEnumerable<string>, string> m_ProtectedMethod;
Run Code Online (Sandbox Code Playgroud)
当我将它作为参数发送到新类时,它不喜欢它,因为该方法被定义为具有三个参数而不是两个参数.
到目前为止,我想到解决这个问题的唯一方法是创建一个重载版本的ProtectedMethod来消除可选参数.
这是唯一的选择还是有另一种方法,因为现在首选的选择是使用可选参数而不是重载方法?
Position我正在使用对象初始值设定项来创建具有如下属性的对象:
var control = new HtmlTextbox(browser)
{
Position = position;
};
Run Code Online (Sandbox Code Playgroud)
据我所知,它与以下内容相同:
var control = new HtmlTextbox(browser);
control.Position = position;
Run Code Online (Sandbox Code Playgroud)
但我想Position在我的构造函数方法中使用初始化属性。Position有没有办法在不提供作为构造函数参数的情况下做到这一点?
我正在尝试构建一个Reg表达式,如果文本框字符串包含两个句点,它将执行我的代码.这是我到目前为止所得到的:
Regex word = new Regex("(\\.){2,}");
if (word.IsMatch(textBoxSearch.Text))
{
//my code here to execute
}
Run Code Online (Sandbox Code Playgroud)
但是,它只在两个句点在一起而不在字符串中的任何位置时执行...