Prashant Pimpale
Prashant Pimpale

Reputation: 10697

Get the value of variable and assign as attribute in C#

There is one API method which looks like:

Task<Response> UpdateImage([AliasAs("banner_image")] StreamPart banner_image=null,[AliasAs("background_image")] StreamPart background_image =null);

But at a time only one image will be there from Frond-End

For ex. I got an image for banner_image then

APIHelper.UpdateImage(banner_image: image)

I getting the attribute information from one variable like,

string key = "banner_image";

So the question is how I can get the value of key and pass it like,

APIHelper.UpdateImage(`dynamic_key_extracted_from_key_variable_value`: image)

Upvotes: 0

Views: 311

Answers (1)

nvoigt
nvoigt

Reputation: 77294

You cannot. That is a language/compiler feature, not available at runtime.

(And for a reason, how should it react when you suddenly pass "xzy"? Have a time-travelling compiler error that reaches you when you compiled it 2 weeks ago?)

Your easiest way around it might be:

StreamPart banner_image = null;
StreamPart background_image = null;

if(key == "banner_image")
{
    banner_image = value;
}

if(key == "background_image")
{
    background_image = value;
}

UpdateImage(banner_image, background_image);

Upvotes: 1

Related Questions