简体   繁体   English

从Xamarin.iOS中的通用基类继承时出错

[英]Error inheriting from generic base class in Xamarin.iOS

I'm developing a Xamarin.iOS application which will connect to a web service via a RESTful API. 我正在开发一个Xamarin.iOS应用程序,它将通过RESTful API连接到Web服务。 I am currently writing the response and business objects for the API client layer, but I have run into a problem. 我目前正在为API客户端层编写响应和业务对象,但我遇到了一个问题。

Consider the following two sample API results: 考虑以下两个示例API结果:

Successful 成功

{  
  "data":{  
    "id":"gwaMR",
    "animated":false,
    "size":11762,
    "link":"http:\/\/i.imgur.com\/gwaMR.jpg"
  },
  "success":true,
  "status":200
}

Erroneous 错误

{  
  "data":{  
    "error":"Unable to find an image with the id, vycVV",
    "request":"\/3\/image\/vycVV",
    "method":"GET"
  },
  "success":false,
  "status":404
}

Because both the successful data information as well as the error details both share the "data" key in their respective scenarios, I have written the following objects in C#: 因为成功的数据信息和错误细节都在各自的场景中共享“数据”键,所以我在C#中编写了以下对象:

public class BaseBusinessObject<D> where D:BaseBusinessObjectData
{
    public D Data { get; set; }
    public int Status { get; set; }
    public bool Success { get; set; }
}

public class BaseBusinessObjectData
{
    public string Error { get; set; }
    public string Method { get; set; }
    public string Request { get; set; }
}

public class ImageObject : BaseBusinessObject<ImageObjectData>
{
}

public class ImageObjectData : BaseBusinessObjectData
{
    public string ID { get; set; }
    public bool Animated { get; set; }
    public int Size { get; set; }
    public string Link { get; set; }
}

When making the API call (using automatic object deserialization with RestSharp ), I provide the object type ImageObject as the expected result type. 在进行API调用时(使用RestSharp自动对象反序列 ),我提供了对象类型ImageObject作为预期的结果类型。 However, I'm not actually able to get that far yet. 但是,我实际上还没有那么远。 When attempting to compile those response object types, I get the following error: 尝试编译这些响应对象类型时,我收到以下错误:

/Users/willseph/GitHub/Repos/AppName/API/API.cs(46,255+): Error CS0305: Using the generic type `AppName.BaseBusinessObject' requires `1' type argument(s) (CS0305) /Users/willseph/GitHub/Repos/AppName/API/API.cs(46,255+):错误CS0305:使用泛型类型`AppName.BaseBusinessObject'需要'1'类型参数(CS0305)

However, when I take this same code and bring it into a standard C# library (still within Xamarin Studio), it compiles just fine. 但是,当我使用相同的代码并将其带入标准的C#库(仍然在Xamarin Studio中)时,它编译得很好。

As a workaround, I am able to remove generics from my code by using hiding and a new Data member: 作为一种解决方法,我可以通过使用隐藏和新的Data成员从我的代码中删除泛型:

public class BaseBusinessObject
{
    public BaseBusinessObjectData Data { get; set; }
    public int Status { get; set; }
    public bool Success { get; set; }
}

public class BaseBusinessObjectData
{
    public string Error { get; set; }
    public string Method { get; set; }
    public string Request { get; set; }
}

public class ImageObject : BaseBusinessObject
{
    public new ImageObjectData Data { get; set; }
}

public class ImageObjectData : BaseBusinessObjectData
{
    public string ID { get; set; }
    public string Link { get; set; }
    public int Size { get; set; }
    public bool Animated { get; set; }
}

This compiles fine and results in expected behavior. 这编译得很好并导致预期的行为。 Does anyone have any insight on what may be causing this issue? 有没有人对可能导致此问题的原因有任何见解?

I tried adding a separate temporary file alongside my workaround containing the original erroneous classes (though renamed to not cause conflict), and it appears to be compiling. 我尝试在我的解决方法中添加一个单独的临时文件,其中包含原始的错误类(虽然重命名为不会导致冲突),但它似乎正在编译。

I'm not sure what the issue was, but I suppose it was some strange fluke or anomaly caused by my computer. 我不确定是什么问题,但我想这是由我的电脑造成的一些奇怪的侥幸或异常。

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

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