質問

Below is the Enumeration defined.

 public enum MessageType
 {
   General = 1,
   Warning = 2,
   Update = 3,
   All = 4
  }

Class Message with the class member as Message Type :

 public class Message
 {

   public MessageType MessageType;

 }

Now, in below method I want to set Message Type from the CRM entity field value.

But I am not getting how to convert string to the enumeration type here.

public Message PopulateMessage(Entity entity)
{

  Message message = new Message();

  message.MessageType =  EntityExtension.ToString(entity,"as_message_type");

  return message;
}

Please suggest.

Thank you, Mittal.

役に立ちましたか?

解決

You can use the Parse-Method or TryParse-Method of the Enum-Type to parse a string to a enum value.

message.MessageType = (MessageType) Enum.Parse(typeOf(MessageType), "as_message_type");

or

MessageType type;

bool parsed = Enum.TryParse<MessageType>("as_message_type", out type);

if (parsed) {
  message.MessageType = type;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top