相关疑难解决方法(0)

在c#中将匿名类型转换为键/值数组?

我有以下匿名类型:

new {data1 = "test1", data2 = "sam", data3 = "bob"}
Run Code Online (Sandbox Code Playgroud)

我需要一个方法来接受它,并在数组或字典中输出键值对.

我的目标是在HttpRequest中将其用作后期数据,因此我最终将连接到以下字符串中:

"data1=test1&data2=sam&data3=bob"
Run Code Online (Sandbox Code Playgroud)

.net c# anonymous-types httpwebrequest

75
推荐指数
4
解决办法
4万
查看次数

如何将类转换为Dictionary <string,string>?

现在我再次解释我对词典的热切期待!从我上一个问题的回答中,这个问题在我脑海中浮现!

现在实际的一点是我可以将类转换为字典吗?

在Dictionary中,我希望我的类属性为KEY,特殊属性的值为VALUE

假设我的班级是

public class Location
{
    public string city { get; set; }
    public string state { get; set; }
    public string country { get; set;
}
Run Code Online (Sandbox Code Playgroud)

现在假设我的数据是

city = Delhi
state = Delhi
country = India
Run Code Online (Sandbox Code Playgroud)

现在你可以轻松理解我的观点!

我想制作字典!那本字典应该是这样的

Dictionary<string,string> dix = new Dictionary<string,string> ();
dix.add("property_name", "property_value");
Run Code Online (Sandbox Code Playgroud)

我可以获得价值!但是我怎样才能得到属性名称(而不是价值)?

我应该编码什么来创建它动态!这应该适用于我想要的每一堂课?

你可以理解这个问题

我怎样才能从特定类中获取属性列表?

c# asp.net dictionary

60
推荐指数
3
解决办法
5万
查看次数

将动态类型转换为字典C#

我有一个看起来像这样的动态对象,

 {
    "2" : "foo",
    "5" : "bar",
    "8" : "foobar"
 }
Run Code Online (Sandbox Code Playgroud)

我该如何将其转换为dictionary

c# json dictionary dynamic json.net

18
推荐指数
4
解决办法
4万
查看次数

将类属性转换为键值对

我有这两个班

public class BuyEstimateResult
{
    public string SubTitle { get; set; }
    public string Value { get; set; }
    public string Formulae { get; set; }
}
public class SellerReportClass
{
    public string Entityname { get; set; }
    public double EntityAmt { get; set; }
    public string Formulae { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我必须使它们应该被转换为

public class KeyValue
{
    public string key {get;set;}
    public string value {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

如果我传递BuyEstimateResult,它的SubTitle应该是key,Value应该是KeyValue Class的值,如果我传递SellerReportClass,那么Entityname应该是key,EntityAmt应该是Value

任何想法怎么做

注意:我将获得该类的列表

c#

5
推荐指数
1
解决办法
4157
查看次数

将任何类转换为keyvaluepair

我有一些(可能很多)类很简单的类

 public class ResultA
    {
        public DateTime Date { get; set; }
        public int Year { get; set; }
        public int Month { get; set; }
        public int Day { get; set; }
    }

 public class ResultB
    {
        public string Number { get; set; }
        public int Count { get; set; }
        public string Update { get; set; }
        public int Jewels{ get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

没有通用接口,但它们没有方法只是属性.

我希望能够将这样的任何类型转换为KeyValuePair<string,string> 具有属性名称的值和设置的值.

无论如何做这件可怕的事情!?

c# deserialization

1
推荐指数
2
解决办法
5266
查看次数