简体   繁体   English

C#XNA Xbox,在这种情况下,可选参数不是可选参数

[英]C# XNA Xbox, in this case optional parameters are not optional

Apparently optional parameters won't work in C# Xna when used on the Xbox, there non suportedness is stated during compilation. 当在Xbox上使用时,可选参数显然在C#Xna中不起作用,因为在编译过程中声明了非替代性。

I have a situation like this: 我有这样的情况:

func(float? a = null, int? b = null)

With large numbers of thease optional parameters that default to the "undefined" value, null. 对于大量的thease可选参数,默认为“未定义”值,为null。 This situation is required. 这种情况是必需的。

In the simplified example above this can be unwrapped, although this isn't as optional parameters allow: 在上面的简化示例中,可以将其解包,尽管这不是可选参数所允许的:

func()
func(float? a)
func(int? b)
func(float? a, int? b)

However with large numbers of parameters this isn't practical. 但是,对于大量参数,这是不切实际的。

Some combinations of parameters definedness isn't permitted and results in paths through the function where I throw argument exceptions, others result in various different things occurring depending on the values of the parameters. 不允许使用参数定义的某些组合,这些组合会导致通过函数抛出我抛出参数异常的路径,而另一些组合会导致各种不同的情况发生,具体取决于参数的值。 This is similar to polymorphism between functions with the same name but is not the same. 这类似于具有相同名称但不相同的函数之间的多态性。

I could alternatively, and probbably most practically, require all the parameters instead. 我可以替代地并且可能最实际地代替所有参数。

func(float? a, int? b)

Then call as so: 然后这样调用:

func(null, 4)

Where the first is undefined. 第一个未定义的位置。

Instead of using one of the above bodges, is there a way to enable optional parameters in C# XNA on Xbox? 有没有一种方法可以在Xbox上的C#XNA中启用可选参数,而不是使用上述功能之一?

I would use old-fashioned overloads that take different numbers of parameters, and forward the calls on from there, along with the "default" values. 我将使用老式重载,该重载采用不同数量的参数,并从那里转发调用以及“默认”值。 Zero code changes to the callers. 零代码更改给调用方。

If the types are outside your control: extension methods to add the overloads. 如果类型超出您的控制范围:扩展方法以添加重载。

I've certainly run on the XBOX with optional parameters, but it required some tweaking of the build settings. 我当然已经在带有可选参数的XBOX上运行,但是它需要对构建设置进行一些调整。 From what I recall it will not work with WP7 however. 我记得它不适用于WP7。

I think it might have been related to the Language Version being used. 我认为这可能与所使用的语言版本有关。

Check out the following links: 查看以下链接:

http://msdn.microsoft.com/en-us/library/ff827884.aspx http://msdn.microsoft.com/en-us/library/ff827884.aspx

http://forums.create.msdn.com/forums/p/54007/327944.aspx http://forums.create.msdn.com/forums/p/54007/327944.aspx

I don't think you can enable the optional parameters. 我认为您无法启用可选参数。 Maybe you can use an alternative. 也许您可以使用其他方法。 like the params keyword that creates an array, you could just pass an object as parameter 就像创建数组的params关键字一样,您可以将一个对象作为参数传递

public class MyParams 
{
     public float? a {get;set;}
     public int? b {get;set;}
}

func(new MyParams { b = 5 });

With that solution, you have all the flexibility you might want. 使用该解决方案,您将拥有所需的所有灵活性。 You can add, remove parameters without thinking of adding/removing new overload. 您可以添加,删除参数,而无需考虑添加/删除新的重载。 And if you remove or edit a parameter, you'll get a compile time error, something you might not get with multiple overload. 而且,如果删除或编辑参数,则会出现编译时错误,这可能是多重重载无法实现的。

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

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