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

  • Home
  • SEARCH
  • 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 7572377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:54:40+00:00 2026-05-30T15:54:40+00:00

Imagine a method with the following signature: public void ExampleMethod(string id, object data, ref

  • 0

Imagine a method with the following signature:

public void ExampleMethod(string id, object data, 
                          ref object A, ref object B, ref object C)
{
  //...
}

The value in data needs to be assigned to either A, B, C or nothing, depending on the value of id. In short, if id == "A" then A = data;

The problem is that the body of this method is typed by a human but the signature is generated at runtime. Because of this, it is not possible to hard-code the logic because it is unknown at design-time how many ref parameters there will be and what they are called. This piece of code may be inserted into any number of methods, each potentially with a different signature, and it has to work in each and every one.

I know how to get all the parameters of the method one is currently in, but I can’t figure out how to assign a value to one of these parameters. What I’m looking for is something like the following:

public void ExampleMethod(string id, object data, 
                          ref object A, ???????, ref object Z)
{
  MethodBase method = MethodBase.GetCurrentMethod();
  foreach (ParameterInfo parameter in method.GetParameters())
  { 
    if (id == parameter.Name)
    {
      // Problem: assign data to parameter.
      return;
    }
  }
}
  • 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-30T15:54:41+00:00Added an answer on May 30, 2026 at 3:54 pm

    You can’t access parameters by name, as you can’t really use reflection on variables/parameters. You might have some options if this was IL, but not really in C#. My advice would be: change the API, maybe something involving either an array or (perhaps better) a dictionary. Consider:

    public void ExampleMethod(string id, object data,
            IDictionary<string,object> args) {
        args[id] = data;
    }
    

    Not sure if that helps… but what you are trying to do is not really reflection-friendly. The other option is to generate this method dynamically, either as a part of your build process, or via IL. Either should be fine. So it could essentially generate (as either C# or (at runtime) IL):

    public void ExampleMethod(string id, object data, 
                              ref object A, ref object B, ref object C)
    {
        switch(id) {
            case "A": A = data; break;
            case "B": B = data; break;
            case "C": C = data; break;
            default: /* do something */
        }
    }
    

    An additional approach: the typed object: say you have:

    public void ExampleMethod(string id, object data, SomeType obj) {...}
    

    where obj is an object with properties such as “A”, “B”, “C”; then what you are trying to generate is:

    switch(id) {
        case "A": obj.A = data; break;
        case "B": obj.B = data; break;
        case "C": obj.C = data; break;
        default: /* do something */
    }
    

    which can of course be done with reflection:

    var prop = obj.GetType().GetProperty(id);
    prop.SetValue(obj, data, null);
    

    or if performance is critical, something like fast-member:

    var wrapped = ObjectAccessor.Create(obj); 
    wrapped[id] = data;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine that you have a method with the following signature: public void DoSomething(Guid id)
In Java 6 , imagine I have the following method signature: public void makeSandwich(Bread
Imagine the following method public void SomeMethod<T>(T param) where T: List<T2> { } It
Imagine the following simple code: public void F<T>(IList<T> values) where T : struct {
Imagine I have a method: void Method(bool parameter){ if(parameter){ // first case } else
Imagine a base class with many constructors and a virtual method public class Foo
I want to create a helper method that I can imagine has a signature
I'm trying to implement the following method: void Ball::DrawOn(Graphics g); The method should draw
Imagine the following: class Repository { private ObservableCollection<ModelClass> _allEntries; public ObservableCollection<ModelClass> AllEntries { get
Imagine someone coding the following: string s = SomeString; s.ToUpper(); We all know that

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.