Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8462059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:03:48+00:00 2026-06-10T14:03:48+00:00

I have started using RestSharp to call an webapi proejct as it seems pretty

  • 0

I have started using RestSharp to call an webapi proejct as it seems pretty easy to use.

I am wanting to build a helper class for all of my crud actions.

I have this so far for a simple PUT request.

 public static IRestResponse Update(object objectToUpdate,string apiEndPoint)
    {
        var client = new RestClient(CreateBaseUrl(null))
        {
            Authenticator = new HttpBasicAuthenticator("user", "Password1")
        };
        var request = new RestRequest(apiEndPoint, Method.PUT);
        request.AddObject(objectToUpdate);
        var response = client.Execute<MyViewModel>(request);
        //var response = client.ExecuteDynamic(request);
        return response;
    }

So the above code works however I have had to hardcode my viewmodel into it

  var response = client.Execute<MyViewModel>(request);

How can I change this so I dont need to know the type of model I am expecting?

I tried using var response = client.ExecuteDynamic(request);
however this throws an exception of

Unable to cast object of type ‘RestSharp.RestResponse’ to type ‘RestSharp.RestResponse`1[System.Object

Im not sure how I am meant to cast my object correctly

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:03:50+00:00Added an answer on June 10, 2026 at 2:03 pm

    I’m not familiar with RestSharp. However, it sounds like generics could help you here. Either your class or method needs to accept a type. For example, the signature of your method would change to

    public static IRestResponse Update<T>(object objectToUpdate,string apiEndPoint)
    

    This would allow you to call the method as:

    Update<MyViewModel>(objectToUpdate, apiEndPoint);
    

    Your implementation would change from your concrete type to:

    var response = client.Execute<T>(request);
    

    Overall you could modify your code to something like this:

     public static IRestResponse Update<T>(object objectToUpdate,string apiEndPoint)
    {
        var client = new RestClient(CreateBaseUrl(null))
        {
            Authenticator = new HttpBasicAuthenticator("user", "Password1")
        };
        var request = new RestRequest(apiEndPoint, Method.PUT);
        request.AddObject(objectToUpdate);
        var response = client.Execute<T>(request);
        //var response = client.ExecuteDynamic(request);
        return response;
    }
    

    Documentation on C# Generics can be found here: http://msdn.microsoft.com/en-us/library/ms379564(v=vs.80).aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have started using Redhat Linux i would like to know how to use
I have started using MonoDevelop for F# on a Mac. It's pretty cool, except
I have started using Jython as it seems to be a excellent language, and
I have started using MVC3 recently, I want to know, Can I use MySQL
I have started using WSO2 Stratos live and started using WSO2 data services server.
I have started using SqLite recently, so I am relatively new to it. I
I have started using https://github.com/omab/django-social-auth and been successfully able to login via twitter, google
I have started using struts .I have hanged in a place ,Code is bellow
I have started using PDO in php for the first time. Here is my
I have started using Linq to SQL for a project im working on and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.