繁体   English   中英

IXamlType接口实现-奇怪的行为

[英]IXamlType interface implementation - weird behavior

IXamlType接口定义如下:

[Guid(0x7920eab1, 0xa2e5, 0x479a, 0xbd, 80, 0x6c, 0xef, 60, 11, 0x49, 0x70), Version(0x6020000), WebHostHidden]
public interface IXamlType
{
    // Methods
    object ActivateInstance();
    void AddToMap([In] object instance, [In] object key, [In] object value);
    void AddToVector([In] object instance, [In] object value);
    object CreateFromString([In] string value);
    IXamlMember GetMember([In] string name);
    void RunInitializer();

    // Properties
    IXamlType BaseType { get; }
    IXamlMember ContentProperty { get; }
    string FullName { get; }
    bool IsArray { get; }
    bool IsBindable { get; }
    bool IsCollection { get; }
    bool IsConstructible { get; }
    bool IsDictionary { get; }
    bool IsMarkupExtension { get; }
    IXamlType ItemType { get; }
    IXamlType KeyType { get; }
    TypeName UnderlyingType { get; }
}

编译Metro应用程序时,将生成XamlTypeInfo.g.cs ,其中包含此接口的实现,如下所示:

[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Windows.UI.Xaml.Build.Tasks", "4.0.0.0")]    
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
internal class XamlSystemBaseType : IXamlType
{
    string _fullName;
    Type _underlyingType;

    public XamlSystemBaseType(string fullName, Type underlyingType)
    {
        _fullName = fullName;
        _underlyingType = underlyingType;
    }

    public string FullName { get { return _fullName; } }

    public Type UnderlyingType
    {
        get
        {
            return _underlyingType;
        }
    }

    virtual public IXamlType BaseType { get { throw new NotImplementedException(); } }
    virtual public IXamlMember ContentProperty { get { throw new NotImplementedException(); } }
    virtual public IXamlMember GetMember(string name) { throw new NotImplementedException(); }
    virtual public bool IsArray { get { throw new NotImplementedException(); } }
    virtual public bool IsCollection { get { throw new NotImplementedException(); } }
    virtual public bool IsConstructible { get { throw new NotImplementedException(); } }
    virtual public bool IsDictionary { get { throw new NotImplementedException(); } }
    virtual public bool IsMarkupExtension { get { throw new NotImplementedException(); } }
    virtual public bool IsBindable { get { throw new NotImplementedException(); } }
    virtual public IXamlType ItemType { get { throw new NotImplementedException(); } }
    virtual public IXamlType KeyType { get { throw new NotImplementedException(); } }
    virtual public object ActivateInstance() { throw new NotImplementedException(); }
    virtual public void AddToMap(object instance, object key, object item)  { throw new NotImplementedException(); }
    virtual public void AddToVector(object instance, object item)  { throw new NotImplementedException(); }
    virtual public void RunInitializer()   { throw new NotImplementedException(); }
    virtual public object CreateFromString(String input)   { throw new NotImplementedException(); }
}

奇怪的是某些接口实现不一致。 如果您尝试在独立的VS项目中编译此实现,则将出现以下错误,这是预期的:

错误CS0738:“ ConsoleApplication28.XamlSystemBaseType”未实现接口成员“ ConsoleApplication28.IXamlType.UnderlyingType”。 'ConsoleApplication28.XamlSystemBaseType.UnderlyingType'无法实现'ConsoleApplication28.IXamlType.UnderlyingType',因为它没有匹配的返回类型'ConsoleApplication28.TypeName'。


我的问题是.g.cs如何编译-我通过在Reflector中打开已编译的程序集进行了确认-它也显示了相同的实现,其中某些属性/方法的签名类型不匹配。

更新:

为了确认我看到的内容,我使用了Reflector并看到了以下内容:

IXamlType.UnderlyingType(来自程序集Windows,版本= 255.255.255.255)定义为:

TypeName UnderlyingType { get; }

在任何Metro应用程序中找到的XamlSystemBaseType都将UnderlyingType定义为:

   public Type UnderlyingType { get; }

鉴于以上所述,我不了解其工作原理!

更新#2

确定-在Reflector中打开Windows.winmd文件时,您会看到:

TypeName UnderlyingType { get; }

但是,如果在Visual Studio中,您可以执行以下操作:

将光标放在下一行的IXamlType上:

内部类XamlSystemBaseType:IXamlType

然后按F12,打开的自动生成的定义文件包含:

    public Type UnderlyingType { get; }

所以在我不知道的场景下肯定发生了一些事情!

将某些类型(如果是)自动映射到其他类型的文档,以及与此相关的规则/属性会很有帮助!

您的问题似乎是找到的接口定义将UnderlyingType属性声明为

TypeName UnderlyingType { get; }

虽然生成的基本实现将其定义为:

public Type UnderlyingType ...

好吧,我在VS中打开XamlTypeInfo.g.cs并导航到IXamlType接口的定义,它实际上将其定义为

Type UnderlyingType { get; }

似乎您找到的接口定义无效。 在查看UnderlyingType属性的文档时,它说该属性的类型在C#中为Type,但在C ++中为TypeName-这可能是您的差异出自何处。

更大的问题是-为什么要尝试在控制台应用程序中对其进行编译? 控制台应用程序不使用WinRT。

看来发生这种情况的原因是所谓的“投影类型”,但是我还没有找到任何正式的文档。 我在这里提出了另一个问题:

WinRT投影类型文档

暂无
暂无

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

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