1

Is it possible to cast object type to type provided like a string?

I am thinking to do like

 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;

    }

and use it like

 var response = await client.PostAsJsonAsync(WebAPIManager.ApiAccountLogin.Url, WebAPIManager.ApiAccountLogin.Container as typeof(WebAPIManager.ApiAccountLogin.ClassType));

I hope you got the idea...

Basically I want to see something generic when I use WebAPIManager...

Any clue guys?

NoWar
  • 36,338
  • 80
  • 323
  • 498
  • 1
    Do you mean `(WebAPIManager.ApiAccountLogin.ClassType)WebAPIManager.ApiAccountLogin.Container`? – Steven Jan 22 '14 at 12:49
  • @Steven yes yes! exactly Is it possible to play around SOMEHOW to get that approach working? – NoWar Jan 22 '14 at 12:50
  • @Steven you can cast to type only - `ClassType` is string – Kamil Budziewski Jan 22 '14 at 12:50
  • 1
    @ClarkKent: Ahh.. your `ClassType` is a string value. Casting is something the compiler does for you, but the value of `ClassType` can change during runtime. It is therefore impossible to cast such thing, since the compiler can never know the type. – Steven Jan 22 '14 at 12:52
  • @BartoszKP Well.. I think I need to cast it coz I have to setup some properties of the class... And coz API method is accepting concrete class and it is not a object... – NoWar Jan 22 '14 at 13:14
  • Why do you need to cast it? Even though the reference is of type object, the instance retains its type. This doesn't make sense really. `PostAsJsonAsync` has a particular signature that you know at compile time. So you can just write `as X` where `X` is the type of parameter this methods expects. – BartoszKP Jan 22 '14 at 13:14
  • @ClarkKent Sorry, I've reposted my comment to make it clearer. – BartoszKP Jan 22 '14 at 13:14
  • @BartoszKP I just want to work around to create some good-looking API methods storage including classes I have to serialize (JSON) and pass to those methods. I am not sure if some good solution is existing... – NoWar Jan 22 '14 at 13:16
  • @ClarkKent Good solution to what exactly? When you have a method `void f(X x)` and you want to call it with parameter `object y`, knowing that runtime-type of `y` is assignable to `X` then you just do `f(y as X)`. There is nothing more to it. If `y` has incompatible type, then you just can't call the method with it. Please explain why can't you write `PostAsJsonAsync(url, WebAPIManager.ApiAccountLogin.Container as X)` where `X` is in `PostAsJsonAsync`'s signature? – BartoszKP Jan 22 '14 at 13:19
  • @Steven It is possible using reflection. But it doesn't make sense here. – BartoszKP Jan 22 '14 at 13:23

1 Answers1

0

Thanks to all!

Like I got I cannot do what I need... So I have refactored code...

  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()
        {
        }
    }

and the usage is

 WebAPIManager apiManager = new WebAPIManager();

   var apiAccountLogin = apiManager.ApiAccountLogin("test01","test01");
                var response = await client.PostAsJsonAsync(apiAccountLogin.Url, apiAccountLogin.Container);
NoWar
  • 36,338
  • 80
  • 323
  • 498