C# 如何在类型输入期间获取属性?

C# 如何在类型输入期间获取属性?,c#,C#,我可能问的问题不对 static void GetEveryThing<t>(string Token) { t x = JsonConvert.DeserializeObject<t>(GetData("Original API Query", Token)); string NextLink = x.nextLink; while (NextLink != null) { t z = JsonConv

我可能问的问题不对

static void GetEveryThing<t>(string Token)
{
    t x = JsonConvert.DeserializeObject<t>(GetData("Original API Query", Token));
    string NextLink = x.nextLink;
    while (NextLink != null)
    {
        t z = JsonConvert.DeserializeObject<t>(GetData(NextLink, Token));
        x.value.AddRange(z.value);
        NextLink = z.nextLink;
    }
}
静态void GetEveryThing(字符串标记)
{
tx=JsonConvert.DeserializeObject(GetData(“原始API查询”,Token));
字符串NextLink=x.NextLink;
while(NextLink!=null)
{
tz=JsonConvert.DeserializeObject(GetData(NextLink,Token));
x、 value.AddRange(z.value);
NextLink=z.NextLink;
}
}
我有七个不同的类需要使用上面的代码。所有类都有属性“count”、“value”和“nextLink”。值是保存实际返回数据的
列表
。nextLink是从API查询返回的,只有下一个查询链接可以继续获取信息

错误是,我无法获取x.nextLink、z.count、z.nextLink、x.value、z.value或z.nextLink。错误代码。我不知道如何使用
来实现这一点

我可以添加有问题的类、GetData或任何其他可能有帮助的内容。我觉得这是一件很基本的事情,我只是不知道如何去做或者问正确的问题

static void GetEveryThing<t>(string Token)
{
    t x = JsonConvert.DeserializeObject<t>(GetData("Original API Query", Token));
    string NextLink = x.nextLink;
    while (NextLink != null)
    {
        t z = JsonConvert.DeserializeObject<t>(GetData(NextLink, Token));
        x.value.AddRange(z.value);
        NextLink = z.nextLink;
    }
}
我该怎么做

编辑:

下面是我尝试使用的两个类

class OfficeData
{
    [JsonProperty("@odata.context")]
    public string context { get; set; }
    public List<OfficeAssocDataRows> value { get; set; }
    [JsonProperty("@odata.count")]
    public int count { get; set; }
    [JsonProperty("@odata.nextLink")]
    public string nextLink { get; set; }
}

class MembersData
{
    [JsonProperty("@odata.context")]
    public string context { get; set; }
    public List<MemberAssocDataRows> value { get; set; }
    [JsonProperty("@odata.count")]
    public int count { get; set; }
    [JsonProperty("@odata.nextLink")]
    public string nextLink { get; set; }
}
OfficeData类
{
[JsonProperty(“@odata.context”)]
公共字符串上下文{get;set;}
公共列表值{get;set;}
[JsonProperty(“@odata.count”)]
公共整数计数{get;set;}
[JsonProperty(“@odata.nextLink”)]
公共字符串nextLink{get;set;}
}
类成员数据
{
[JsonProperty(“@odata.context”)]
公共字符串上下文{get;set;}
公共列表值{get;set;}
[JsonProperty(“@odata.count”)]
公共整数计数{get;set;}
[JsonProperty(“@odata.nextLink”)]
公共字符串nextLink{get;set;}
}
---这个问题已经结束了,请留下一个便条

第二次编辑:

我几乎完成了界面设置

interface IAPIDataInterface
{
    [JsonProperty("@odata.context")]
    public string context { get; set; }
    //public List<T> value { get; set; } <-- How do I get this to pass through?
    [JsonProperty("@odata.count")]
    public int count { get; set; }      
    [JsonProperty("@odata.nextLink")]
    public string nextLink { get; set; }
}

class OfficeData : IAPIDataInterface
{
    [JsonProperty("@odata.context")]
    public string context { get; set; }
    public List<OfficeAssocDataRows> value { get; set; }
    [JsonProperty("@odata.count")]
    public int count { get; set; }
    [JsonProperty("@odata.nextLink")]
    public string nextLink { get; set; }
}

class MembersData : IAPIDataInterface
{
    [JsonProperty("@odata.context")]
    public string context { get; set; }
    public List<MemberAssocDataRows> value { get; set; }
    [JsonProperty("@odata.count")]
    public int count { get; set; }
    [JsonProperty("@odata.nextLink")]
    public string nextLink { get; set; }
}
接口IAPIDATA接口
{
[JsonProperty(“@odata.context”)]
公共字符串上下文{get;set;}
//公共列表值{get;set;}将“t”转换为所需的对象

YourObject yourObject = t as YourObject

您需要将您的内容反序列化以从公共接口继承。然后,您可以在泛型方法上设置约束,将其约束到实现该接口的接受类。此时,您将可以访问所需的公共成员。请参阅下面的人为示例

class Program
    {
        static void Main(string[] args)
        {
            var bar = new Bar
            {
                Count = 10,
                Value = "Bar",
                NextLink = "Bleh"
            };
            var fooBar = new FooBar
            {
                Count = 20,
                Value = "FooBar",
                NextLink = "Meh"
            };
            var serializedBar = JsonConvert.SerializeObject(bar);
            var serializedFooBar = JsonConvert.SerializeObject(fooBar);
            GetEverything<Bar>(serializedBar);
            GetEverything<FooBar>(serializedFooBar);
        }

        static void GetEverything<T>(string thingToDeserialize) where T : IFoo
        {
            T x = JsonConvert.DeserializeObject<T>(thingToDeserialize);
            Console.WriteLine(x.Value);
            Console.WriteLine(x.NextLink);
            Console.WriteLine(x.Count);
        }
    }

    public interface IFoo
    {
        public int Count { get; set; }
        public string Value { get; set; }
        public string NextLink { get; set; }
    }

    public class Bar : IFoo
    {
        public int Count { get; set; }
        public string Value { get; set; }
        public string NextLink { get; set; }
    }

    public class FooBar : IFoo
    {
        public int Count { get; set; }
        public string Value { get; set; }
        public string NextLink { get; set; }
    }
类程序
{
静态void Main(字符串[]参数)
{
变量条=新条
{
计数=10,
Value=“Bar”,
NextLink=“Bleh”
};
var fooBar=新fooBar
{
计数=20,
Value=“FooBar”,
NextLink=“Meh”
};
var serializedBar=JsonConvert.SerializeObject(bar);
var serializedFooBar=JsonConvert.SerializeObject(fooBar);
GetEverything(序列化dbar);
GetEverything(序列化Foobar);
}
静态void GetEverything(字符串thingToDeserialize),其中T:IFoo
{
tx=JsonConvert.DeserializeObject(thingToDeserialize);
控制台写入线(x.Value);
控制台写入线(x.NextLink);
控制台写入线(x.Count);
}
}
公共接口IFoo
{
公共整数计数{get;set;}
公共字符串值{get;set;}
公共字符串NextLink{get;set;}
}
公共类酒吧:IFoo
{
公共整数计数{get;set;}
公共字符串值{get;set;}
公共字符串NextLink{get;set;}
}
公共类FooBar:IFoo
{
公共整数计数{get;set;}
公共字符串值{get;set;}
公共字符串NextLink{get;set;}
}

t
一种众所周知的类型吗?也许你可以定义一个允许类型约束的接口。我试图避免使用同一个函数的7个副本,只是类类型不同。我已经看到了。JsonConvert.DeserializeObject可以做到这一点。
其中t:ISomeInterface
其中t:BaseClass
我已经更新了这个问题。我无法使用“static void GetEveryThing(string Token)where T:MembersData,OfficeData”,但我可以只使用一个类或另一个类。如果可能,我将如何实现这一点?我将如何使用;GetEveryThing(“XXX”);和GetEveryThing(“XXX”);可选(这太复杂了,我不想用一个例子来解释)你可以做一些疯狂的反思,对类型和属性做出假设。但是如果没有令人惊讶的理由,我不会这么做。