简体   繁体   English

我有一个ac#class返回一些字段...但是我希望它返回一个自定义对象列表(我想!!!)

[英]I have a c# class that returns some fields…but I want it to return a list of custom objects (I think!!!)

This is a very noob question, but I have been googling and can't seem to work out the solution myself. 这是一个非常noob的问题,但我一直在谷歌搜索,似乎无法解决自己的解决方案。
I have created a class that has a number of fields (below). 我创建了一个包含许多字段的类(如下所示)。 I am grabbing data from a .JSON file. 我正在从.JSON文件中获取数据。

public class WeatherData
{
    //WeatherDatas
    public string airtemp { get; set; }
    public string apparenttemp { get; set; }
    public string windspeedkph { get; set; }
    public string windgustskph { get; set; }
    public string humidity { get; set; }
    public string dewpoint { get; set; }
    public string deltaT { get; set; }
    public string pressure { get; set; }

    public WeatherData(string json, int index)
    {
        JObject jsonObject = JObject.Parse(json);
        JToken jObbs = jsonObject["observations"];
        JToken jData = jObbs["data"];            
        airtemp = (string)jData[index]["air_temp"];
        apparenttemp = (string)jData[index]["apparent_t"];
        windspeedkph = (string)jData[index]["wind_spd_kmh"];
        windgustskph = (string)jData[index]["gust_kmh"];
        humidity = (string)jData[index]["rel_hum"];
        pressure = (string)jData[index]["press_qnh"];
    }
}

Using the above I can get the "airtemp" from "WeatherData.airtemp". 使用上面我可以从“WeatherData.airtemp”获得“airtemp”。 But due to some bells and whistles I want to add later what I really want to do is return not just the airtemp value but a field/property indicating the type of the value. 但是由于一些钟声和口哨,我想稍后添加我真正想做的是不仅返回airtemp值而且还返回指示值类型的字段/属性。 For example something like: 例如:

WeatherData.WeatherDatas.airtemp.value & WeatherData.WeatherDatas.airtemp.type WeatherData.WeatherDatas.airtemp.valueWeatherData.WeatherDatas.airtemp.type

Where .value would be air temp and .type be the string "airtemp". 其中.value是air temp而.type是字符串“airtemp”。

I just can't seem to work out how to describe to google what I am trying to do. 我似乎无法弄清楚如何描述谷歌我想做什么。

I think you may want to look into using a dictionary. 我想你可能想看一下使用字典。 Dictionaries in c# use Key, Value pairs, so you may wish to create a dictionary of Your key values would be the type, such as airtemp and the value values would be the value such as 32.54. c#中的字典使用Key,Value对,因此您可能希望创建一个字典,您的键值将是类型,例如airtemp,值的值将是32.54等值。

I recommend looking up this page if you're new to C# and want to learn Dictionaries, or any other cool C# things. 如果您是C#的新手,我建议您查看此页面,并希望学习字典或任何其他很酷的C#内容。 http://www.dotnetperls.com/dictionary http://www.dotnetperls.com/dictionary

What you can do here is to use a struct to represent your instance attributes. 你在这里可以做的是使用struct来表示你的实例属性。 St like this: 像这样:

public struct Data
{
    public Type type; // if you want a string type (I don't know why) you can use a string type
    public string Value;
}

Then you class will look like this: 然后你的课将如下所示:

public class WeatherData
{
    //WeatherDatas
    public Data airtemp { get; set; }
    ...

    public WeatherData(string json, int index)
    {
        JObject jsonObject = JObject.Parse(json);
        JToken jObbs = jsonObject["observations"];
        JToken jData = jObbs["data"];            
        airtemp.Value = new Data((string)jData[index]["air_temp"], typeof(string));

        ...
    }
}

Note that: If you have too many properties in your class, consider using a class istead of struct. 请注意:如果您的类中有太多属性,请考虑使用类而不是struct。

You need to explore types: 你需要探索类型:

Type myType;
Object windy = "strong";
myType = windy.GetType();

Then use typeof() to see what you've got. 然后使用typeof()来查看你得到了什么。

Not sure that dictionary is the way forward. 不确定字典是前进的方向。

You can get property name without change your structure: 您可以在不更改结构的情况下获取属性名称:

by example: PropertyUtil<WeatherData>.GetName(x => x.airtemp); 通过示例: PropertyUtil<WeatherData>.GetName(x => x.airtemp);

https://stackoverflow.com/a/6043028/440030 (source code) https://stackoverflow.com/a/6043028/440030 (源代码)

public static class PropertyUtil<TSource>
{
    public static string GetPropertyName<TResult>(
        Expression<Func<TSource, TResult>> propertyExpression)
    {
       return (propertyExpression.Body as MemberExpression).Member.Name;
    }
}

You can create your own data type, instead of using string use something else Like; 您可以创建自己的数据类型,而不是使用字符串使用其他类似的;

public class UserString
{
    public Type _type{ get; set; }
    public string value{ get; set; }

    public UserString()
{
   _type = null;
   value = string.Empty;
}
}

public class WeatherData
{
    //WeatherDatas
    public UserString airtemp { get; set; }
    public UserString apparenttemp { get; set; }
    public UserString windspeedkph { get; set; }
    public UserString windgustskph { get; set; }
    public UserString humidity { get; set; }
    public UserString dewpoint { get; set; }
    public UserString deltaT { get; set; }
    public UserString pressure { get; set; }

    public WeatherData(string json, int index)
    {
        JObject jsonObject = JObject.Parse(json);
        JToken jObbs = jsonObject["observations"];
        JToken jData = jObbs["data"];            
        airtemp.value = (string)jData[index]["air_temp"];
//        airtemp._type = typeof(string); //Something like that.
        apparenttemp = (string)jData[index]["apparent_t"];
        windspeedkph = (string)jData[index]["wind_spd_kmh"];
        windgustskph = (string)jData[index]["gust_kmh"];
        humidity = (string)jData[index]["rel_hum"];
        pressure = (string)jData[index]["press_qnh"];
    }
}

暂无
暂无

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

相关问题 C#中的CSV阅读,我只想阅读一些字段 - CSV reading in C#, I just want to read some fields C#创建一个Class,将对象作为成员变量? 我认为对象是垃圾收集? - C# creating a Class, having objects as member variables? I think the objects are garbage collected? 在 C# 中,如果我有通用对象,我如何在这些对象上通用地指定获取和设置字段? - In C#, if I have generic objects, how can I generically specify getting and setting fields on those objects? 我有一个对象列表,我想按对象的ID对它进行排序吗? - I have a list of Objects and I want to sort it by the objects's ID? 我想删除 C# 中的一些文本 - I want to remove some text in C# 我在 C# 中有一个列表列表,我想使用 listview 分组在 xaml 中绑定列表 - I have a list of list in C# and i want to bind the list in xaml using listview grouping C#债券:我的债券结构中可以有自定义类的列表吗? - C# Bond: Can I have List of custom class in my bond struct? 我有一个 JSON 想在我的 Unity C# 应用程序中使用,其中某些属性过载 - I have a JSON that I want to use in my Unity C# application where some properties are overloaded 我有1个字符串xml,我想把它转换成class C#,我该怎么办? - I have 1 string xml, I want to convert it to class C#, what should I do? 我如何像在C ++中那样访问C#中的类成员? - How do I access members of a class in C# like I think I could do in c++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM