将对象类型转换为类似字符串提供的类型

时间:2014-01-22 12:47:21

标签: c# .net

是否可以将对象类型转换为类似字符串提供的类型?

我想要像

那样做
 public static class WebAPIManager
    {
        public static WebAPIManagerItem ApiAccountLogin 
        { 
            get 
            {
                return new WebAPIManagerItem { Url = "api/Account/Login", Container = new LoginBindingModel(),  ClassType="LoginBindingModel" }; 
            } 
        }
    }

    public class WebAPIManagerItem 
    {
        public string Url;

        public object Container;

        public string ClassType;

    }

并像

一样使用它
 var response = await client.PostAsJsonAsync(WebAPIManager.ApiAccountLogin.Url, WebAPIManager.ApiAccountLogin.Container as typeof(WebAPIManager.ApiAccountLogin.ClassType));

我希望你有这个主意......

基本上我想在使用WebAPIManager ...

时看到一些通用的东西

任何线索的人?

1 个答案:

答案 0 :(得分:0)

感谢所有人!

就像我得到的那样我不能做我需要的...... 所以我重构了代码......

  public   class WebAPIManager
    {
        public   ApiAccountLogin ApiAccountLogin(string password, string username)
        {
            return new ApiAccountLogin { Url = "api/Account/Login", Container = new LoginBindingModel() { Password = password, UserName = username } };
        }
    }

    public class ApiAccountLogin
    {
        public string Url;

        public LoginBindingModel Container;

        public ApiAccountLogin(string password, string username)
        {
            Container.Password = password;
            Container.UserName = username;
        }

        public ApiAccountLogin()
        {
        }
    }

,用法是

 WebAPIManager apiManager = new WebAPIManager();

   var apiAccountLogin = apiManager.ApiAccountLogin("test01","test01");
                var response = await client.PostAsJsonAsync(apiAccountLogin.Url, apiAccountLogin.Container);