[英]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.value
& WeatherData.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.