Noob C#类声明问题

时间:2017-02-15 14:18:13

标签: c# visual-studio intellisense

所以我使用json2csharp

创建了一个类
    public class ResponseType
    {
        public class Query
        {
            public string q { get; set; }
            public object sku { get; set; }
            public int limit { get; set; }
            public object reference { get; set; }
            public object mpn_or_sku { get; set; }
            public string mpn { get; set; }
            public object brand { get; set; }
            public string __class__ { get; set; }
            public int start { get; set; }
            public object seller { get; set; }
        }

        public class Request
        {
            public bool exact_only { get; set; }
            public string __class__ { get; set; }
            public List<Query> queries { get; set; }
        }

        public class Seller
        {
            public string display_flag { get; set; }
            public bool has_ecommerce { get; set; }
            public string name { get; set; }
            public string __class__ { get; set; }
            public string homepage_url { get; set; }
            public string id { get; set; }
            public string uid { get; set; }
        }

        public class Prices
        {
            public List<List<object>> USD { get; set; }
            public List<List<object>> JPY { get; set; }
            public List<List<object>> CNY { get; set; }
        }

        public class Offer
        {
            public string sku { get; set; }
            public string packaging { get; set; }
            public string on_order_eta { get; set; }
            public string last_updated { get; set; }
            public int? order_multiple { get; set; }
            public int in_stock_quantity { get; set; }
            public string eligible_region { get; set; }
            public int? moq { get; set; }
            public int? on_order_quantity { get; set; }
            public object octopart_rfq_url { get; set; }
            public string __class__ { get; set; }
            public Seller seller { get; set; }
            public string product_url { get; set; }
            public object factory_order_multiple { get; set; }
            public string _naive_id { get; set; }
            public int? factory_lead_days { get; set; }
            public Prices prices { get; set; }
            public bool is_authorized { get; set; }
            public bool is_realtime { get; set; }
        }

        public class Brand
        {
            public string homepage_url { get; set; }
            public string __class__ { get; set; }
            public string name { get; set; }
            public string uid { get; set; }
        }

        public class Manufacturer
        {
            public string homepage_url { get; set; }
            public string __class__ { get; set; }
            public string name { get; set; }
            public string uid { get; set; }
        }

        public class Item
        {
            public List<Offer> offers { get; set; }
            public string uid { get; set; }
            public string mpn { get; set; }
            public List<object> redirected_uids { get; set; }
            public Brand brand { get; set; }
            public string octopart_url { get; set; }
            public string __class__ { get; set; }
            public Manufacturer manufacturer { get; set; }
        }

        public class Result
        {
            public List<Item> items { get; set; }
            public int hits { get; set; }
            public string __class__ { get; set; }
            public object reference { get; set; }
            public object error { get; set; }
        }

        public class RootObject
        {
            public int msec { get; set; }
            public Request request { get; set; }
            public string __class__ { get; set; }
            public List<Result> results { get; set; }
        }
    }

问题是在设计时,当我声明一个具有我类的类型的变量时:

ResponseType Response = new ResponseType();

Intellisense不允许我访问子类RootObject.results列表。它仅显示EqualsGetHashCodeGetTypeToString。我假设我在班级宣言中做错了。

提前谢谢!

编辑 - 我对C Sharp很新。我试图解析REST API的响应。我使用了Rest API提供的JSON,并使用json2csharp将其转换为类。我的意图是做这样的事情

在函数返回中:

    public ResponseType ExecuteSearch(String PartNumber)
    {

〜生成req的所有代码

        // Perform the search and obtain results
        var data = client.Execute(req).Content;
        JSON = data;

    return JsonConvert.DeserializeObject<ResponseType>(data);
    }

然后能够作为函数之外的对象访问响应

编辑2:

我弄清楚我做了什么。而不是在ResponseType中嵌套所有内容,我应该简单地将RootObject重命名为ResponseType。

3 个答案:

答案 0 :(得分:6)

  

Intellisense不允许我访问子类RootObject.results列表

这是因为属性results不是静态的,您尝试以这种方式访问​​它。通过ClassName.PropertyName访问静态属性。有关static variables的更多信息,请查看链接。

  

它只显示Equals,GetHashCode,GetType和ToString

这是C#中每个对象都继承自类object的基本方法集。这就是你能看到它的原因。

Intellisense将允许您这样做:

ResponseType.RootObject ro = new ResponseType.RootObject();
ro.results.First();

因为您需要该类的实例来访问属性results

  

我假设我在班级宣言中做错了。

这取决于。基本上,如果编译器没有抱怨,那么你声明你的类应该是。但是属性声明会命令您以特定方式访问它们。因此,如果您仍希望使用results访问RootObject.results,则需要static

public class RootObject
{
    public static List<Result> results { get; set; }
}

但请注意,此列表只会存在一次!并不是每个RootObject实例的个体!由于你有嵌入式类,你需要像这样调用它:

ResponseType.RootObject.results.WhatEver();

修改

我想您希望在RootObject类型的Object中获取类型为ResponseType的Object。如果我是对的,则没有必要在ResponseType内声明类,但你必须在其中声明每种类型的变量,如:

public class ResponseType
{
    public RootObject MyRootObject{ get; set; }
}

public class RootObject
{
    public int msec { get; set; }
    public Request request { get; set; }
    public string __class__ { get; set; }
    public List<Result> results { get; set; }
}

现在,您将能够访问results对象中的ResponseType变量:

ResponseType rt = new ResponseType();
rt.MyRootObject.results.WhatEver();

有关如何将JSON反序列化为类的更多信息,请阅读Deserialize JSON to C# Classes帖子

答案 1 :(得分:0)

1)具有ResponseType类的对象不包含任何字段(事件静态字段)。 2)您声明了ResponseType个对象,但resultsRootObject个对象的字段。

因此,如果您想使用results,您应该执行以下操作:

ResponseType.RootObject rootObject = new ResponseType.RootObject();
rootObject.results.DoWork();

答案 2 :(得分:0)

以下是我认为你要做的事情。如果这是某种数据传输对象(DTO),我只会在这种形式下使用它,否则对于将在代码中使用的类来说是非常糟糕的做法(主要是因为所有字段上的公共getter和setter)以及与类名相匹配的字段名称),但它确实显示了您的主要错误,即需要在主类之外定义类,如果您需要在顶级类中使用该类类型,则需要定义公共类字段来访问它。

public class ResponseType
{
    public Query Query { get; set; }
    public Request Request { get; set; }
    public Seller Seller { get; set; }
    public Prices Prices { get; set; }
    public Offer Offer { get; set; }
    public Brand Brand { get; set; }
    public Manufacturer Manufacturer { get; set; }
    public Item Item { get; set; }
    public Result Result { get; set; }
    public RootObject RootObject { get; set; }
}

public class Query
{
    public string q { get; set; }
    public object sku { get; set; }
    public int limit { get; set; }
    public object reference { get; set; }
    public object mpn_or_sku { get; set; }
    public string mpn { get; set; }
    public object brand { get; set; }
    public string __class__ { get; set; }
    public int start { get; set; }
    public object seller { get; set; }
}
public class Request
{
    public bool exact_only { get; set; }
    public string __class__ { get; set; }
    public List<Query> queries { get; set; }
}
public class Seller
{
    public string display_flag { get; set; }
    public bool has_ecommerce { get; set; }
    public string name { get; set; }
    public string __class__ { get; set; }
    public string homepage_url { get; set; }
    public string id { get; set; }
    public string uid { get; set; }
}
public class Prices
{
    public List<List<object>> USD { get; set; }
    public List<List<object>> JPY { get; set; }
    public List<List<object>> CNY { get; set; }
}
public class Offer
{
    public string sku { get; set; }
    public string packaging { get; set; }
    public string on_order_eta { get; set; }
    public string last_updated { get; set; }
    public int? order_multiple { get; set; }
    public int in_stock_quantity { get; set; }
    public string eligible_region { get; set; }
    public int? moq { get; set; }
    public int? on_order_quantity { get; set; }
    public object octopart_rfq_url { get; set; }
    public string __class__ { get; set; }
    public Seller seller { get; set; }
    public string product_url { get; set; }
    public object factory_order_multiple { get; set; }
    public string _naive_id { get; set; }
    public int? factory_lead_days { get; set; }
    public Prices prices { get; set; }
    public bool is_authorized { get; set; }
    public bool is_realtime { get; set; }
}
public class Brand
{
    public string homepage_url { get; set; }
    public string __class__ { get; set; }
    public string name { get; set; }
    public string uid { get; set; }
}
public class Manufacturer
{
    public string homepage_url { get; set; }
    public string __class__ { get; set; }
    public string name { get; set; }
    public string uid { get; set; }
}
public class Item
{
    public List<Offer> offers { get; set; }
    public string uid { get; set; }
    public string mpn { get; set; }
    public List<object> redirected_uids { get; set; }
    public Brand brand { get; set; }
    public string octopart_url { get; set; }
    public string __class__ { get; set; }
    public Manufacturer manufacturer { get; set; }
}
public class Result
{
    public List<Item> items { get; set; }
    public int hits { get; set; }
    public string __class__ { get; set; }
    public object reference { get; set; }
    public object error { get; set; }
}
public class RootObject
{
    public int msec { get; set; }
    public Request request { get; set; }
    public string __class__ { get; set; }
    public List<Result> results { get; set; }
}