Is there a way to add optional parameters to C# 3.0 like there will be in C# 4.0? I gotta have this feature, I just can't wait!
有没有办法在C#3.0中添加可选参数,就像在C#4.0中一样?我得拥有这个功能,我等不及了!
Edit:
If you know a work-around/hack to accomplish this, post it also. Thanks!
如果你知道一个解决方法/黑客来完成这个,也发布它。谢谢!
5 个解决方案
#1
You can use an anonymous type and reflection as a workaround to named parameters:
您可以使用匿名类型和反射作为命名参数的变通方法:
public void Foo<T>(T parameters)
{
var dict = typeof(T).GetProperties()
.ToDictionary(p => p.Name,
p => p.GetValue(parameters, null));
if (dict.ContainsKey("Message"))
{
Console.WriteLine(dict["Message"]);
}
}
So now I can call Foo like this:
所以现在我可以像这样打电话给Foo:
Foo(new { Message = "Hello World" });
... and it will write my message.
......它会写下我的信息。
Basically I'm extracting all the properties from the anonymous type that was passed, and converting them into a dictionary of string and object (the name of the property and its value).
基本上我是从传递的匿名类型中提取所有属性,并将它们转换为字符串和对象的字典(属性的名称及其值)。
#2
Unfortunately, no. You will need the C# 4.0 compiler to support this. If you want optional parameters on the .NET platform today, you can try VB .NET or F#.
很不幸的是,不行。您将需要C#4.0编译器来支持此功能。如果您想在.NET平台上使用可选参数,可以尝试使用VB .NET或F#。
#3
There's always method overloading. :)
总是有方法重载。 :)
#4
Like Dustin said, optional parameters are coming in C# 4.0. One kind of crappy way to simulate optional parameters would be to have an object[] (or more strongly-typed array) as your last argument.
就像达斯汀所说,可选参数将在C#4.0中出现。模拟可选参数的一种蹩脚方法是将object [](或更强类型的数组)作为最后一个参数。
#5
One could also use variable arguments as option parameters. An example of the way this works is string.Format().
也可以使用变量参数作为选项参数。这种方式的一个例子是string.Format()。
See here:
http://blogs.msdn.com/csharpfaq/archive/2004/05/13/131493.aspx
#1
You can use an anonymous type and reflection as a workaround to named parameters:
您可以使用匿名类型和反射作为命名参数的变通方法:
public void Foo<T>(T parameters)
{
var dict = typeof(T).GetProperties()
.ToDictionary(p => p.Name,
p => p.GetValue(parameters, null));
if (dict.ContainsKey("Message"))
{
Console.WriteLine(dict["Message"]);
}
}
So now I can call Foo like this:
所以现在我可以像这样打电话给Foo:
Foo(new { Message = "Hello World" });
... and it will write my message.
......它会写下我的信息。
Basically I'm extracting all the properties from the anonymous type that was passed, and converting them into a dictionary of string and object (the name of the property and its value).
基本上我是从传递的匿名类型中提取所有属性,并将它们转换为字符串和对象的字典(属性的名称及其值)。
#2
Unfortunately, no. You will need the C# 4.0 compiler to support this. If you want optional parameters on the .NET platform today, you can try VB .NET or F#.
很不幸的是,不行。您将需要C#4.0编译器来支持此功能。如果您想在.NET平台上使用可选参数,可以尝试使用VB .NET或F#。
#3
There's always method overloading. :)
总是有方法重载。 :)
#4
Like Dustin said, optional parameters are coming in C# 4.0. One kind of crappy way to simulate optional parameters would be to have an object[] (or more strongly-typed array) as your last argument.
就像达斯汀所说,可选参数将在C#4.0中出现。模拟可选参数的一种蹩脚方法是将object [](或更强类型的数组)作为最后一个参数。
#5
One could also use variable arguments as option parameters. An example of the way this works is string.Format().
也可以使用变量参数作为选项参数。这种方式的一个例子是string.Format()。
See here:
http://blogs.msdn.com/csharpfaq/archive/2004/05/13/131493.aspx