如何使用x:Object?何时使用? [英] How to use x:Object and when?
问题描述
将标记扩展名x:Arguments
的参数传递给文档,我可以看到具体数据类型的使用,例如x:Int32
或x:String
,但是x:Object
的用例是什么?而且,要使用它,应在标签之间放置什么? <x:Object> ??? </x:Object>
When passing arguments by the markup extension x:Arguments
to non default constructors as specified by the docs, I can see the use of concrete data types such as x:Int32
or x:String
, but what's the use case of x:Object
? And what's more, to use it, what should be put between the tag? <x:Object> ??? </x:Object>
对于整数或字符串,很自然地将它们视为变量分配,然后将变量传递给构造函数.但是对于Object
,这样的变量通常是由另一个用户定义的类构造的,那么如何指定要创建的类呢?
In the case of integer or string, it is natural to think them as an variable assignment and the variables then get passed to the constructors. But in the case of Object
, such an variable normally is constructed by another user defined class, so how to specify what class you want to create?
推荐答案
技术上,您可以将x:Object
作为构造函数参数传递. x:Object
对应于System.Object
,它确实具有默认构造函数.所以如果你有
Technically you can pass x:Object
as constructor argument. x:Object
corresponds to System.Object
which does have default constructor. So if you have
public class MyClass {
public MyClass(object arg) {
}
}
您可以这样构造它:
<my:MyClass>
<x:Arguments>
<x:Object />
</x:Arguments>
</my:MyClass>
但这不是很有用,因为它将对应于
But this is not quite useful, because it will correspond to
new MyClass(new object());
由于Object
没有任何其他构造函数-您不能以任何其他有意义的方式构造它.因此,当构造函数需要类型为object的参数时-您不想使用x:Object
而是实际的类型:
Since Object
does not have any other constructors - you cannot construct it in any other meaningful way. So when constructor expects argument of type object - you don't want to use x:Object
but instead actual type:
<my:MyClass>
<x:Arguments>
<x:String>string</x:String>
</x:Arguments>
</my:MyClass>
对应于
new MyClass("string");
这篇关于如何使用x:Object?何时使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!