0

I have roughly 20+ enums that are all the same, but coming from different web service libraries, so when used they all need they're fully namespaced reference like this below

  1. WebServices_Production_TrustOnline_ARM.yesnounspecified
  2. WebServices_Development_TrustOnline_ARM.yesnounspecified
  3. WebServices_Production_TrustOnline_BW.yesnounspecified
  4. WebServices_Development_TrustOnline_BW.yesnounspecified
  5. etc
  6. etc up to 20+

Where each enum is like this below

public enum yesnounspecified
{
    unspecified,
    yes,
    no,
}

In my method I set all the values of a dynamic object, also from each of the different web services and I need to be able to set the enum to (yes/no) but not sure how to pass it in and use it

ex.

MyMethod("something", new WebServices_Production_TrustOnline_ARM.objectToFill());


public void MyMethod(string otherParam, dynamic someObject) {

    // 'cigarettes' is a 'yesnospecified' enum
    someObject.cigarettes = // need to set the enum value (yes/no) here but not sure how to pass it in and set it

}
4
  • 2
    Do they all have the same integer values? (yesnounspecified)(int)x; Commented Dec 24, 2019 at 2:38
  • yes, I believe so as none of them actually specify any int values. They are in the web service file just as I have shown in my example, with just the names, no int values specified Commented Dec 24, 2019 at 2:41
  • @user1186050 you want to set someObject.cigarettes = yes/no from Enum ...right? correct me if am wrong Commented Dec 24, 2019 at 2:49
  • that is correct Commented Dec 24, 2019 at 2:52

1 Answer 1

1

Using reflection

public void MyMethod(string otherParam, object someObject) 
{
    var prop = someObject.GetType().GetProperty("cigarettes");
    var enumType = prop.PropertyType;
    prop.SetValue(someObject, Enum.Parse(enumType, "yes"), null);
}

This gets the information about the "cigarettes" property using reflection, including the enum type. Enum.Parse(enumType, string) or Enum.ToObject(enumType, intValue) can then be used to construct a value of this type. SetValue writes the constructed value to the property.

Using generics

An alternative - if the value is fixed - would be to make the method generic and pass the enum value to the method:

public void MyMethod<T>(string otherParam, dynamic someObject, T value) 
{
    someObject.cigarettes = value;
}

Call: MyMethod("x", obj, yesnounspecified.yes);

Sign up to request clarification or add additional context in comments.

9 Comments

And this will actually extract the enum from the dynamic object? So that I can use it?
Yes, it will do. Performance will not be the best, so if you call this method very often it would be better to write 20+ variants of it.
no it only gets called a couple times a week, but wouldn't be eaier to just pass in some kind of generic? I'm not that experienced with generics but it seems like that would be the best solution, but I don't know how it would be done!
like what if I pass it in as a dynamic?
Added an alternative to my answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.