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 3934684
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:44:37+00:00 2026-05-19T23:44:37+00:00

I am trying to update public parameters of a known type MyClass from a

  • 0

I am trying to update public parameters of a known type MyClass from a NameValueCollection (see code below). This is possible in MVC3, the Controller class has an UpdateModel method which does exactly this with the Request.Params. I would like to do this outside of the Controller though.

Any thoughts?

public class MyClass
{
    public string MyParam { get; set; }
}

…

var values = new NameValueCollection() { { "MyParam", "any string value for my param" } };
var myInstance = new MyClass();
Update(myInstance, values);

Cheers,
T

  • 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-05-19T23:44:38+00:00Added an answer on May 19, 2026 at 11:44 pm

    You should use reflection to perform this task:

    This code is provided without proper validation, and it should be put into a method.

    var type = myInstance.GetType();
    foreach(var keyValue in values)
    {
       type.InvokeMember(keyValue.Key,BindingFlags.SetProperty,null,myInstance,  new object[]{keyValue.Value});    
    
    }
    

    There could be an error in this code, but even if it’s the case, the general idea is here.
    2 remarks: This code will fail miserably if the property doesn’t exist on MyClass, or if the property type cannot be assigned from a string. It would therefore require proper validation (I know, I am repeating myself ).

    You could use expression tree to perform the job too, particularly if you are setting a lot of values on the same known type, as expression trees can be compiled.

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

Sidebar

Related Questions

Possible Duplicate: C# Update Table using SqlCommand.Parameters I'm trying to update an SQL Server
I am trying update my table from c# ado.net with this function.I am using
I am trying update text views while this loop is processing. Here is my
Im trying to update a table with the following code. If I change WHERE
When trying to update a subversion working copy from Netbeans, I get the following
Im trying to update the contents of an element after running some php code.
I'm trying to update my code to use cv2.SURF() as opposed to cv2.FeatureDetector_create(SURF) and
I am trying to update a database with values from a csv file, the
public IEnumerable<T> ExecuteStoredProcedure<T>(params object[] parameters) { Type genericType = typeof(T); string commandthing = genericType.Name.Replace(Result,
I am trying to get the textchanged value from textbox to update in my

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.