Search code examples
c#.netasp.net-core-2.0mailchimpmandrill

Is is possible to set value in property without a setter?


I am implementing mandrill in .net application using this package (https://github.com/shawnmclean/Mandrill-dotnet) Now there is a model "EmailMessage" that is used to send an email. Inside of EmailMessage model there is a property "GlobalMergeVars":

   [JsonProperty("global_merge_vars")]
   public List<MergeVar> GlobalMergeVars { get; }

Now we can see that there is no setter for GlobalMergeVars but I have to set a value for this. If we go to this link (https://mandrillapp.com/api/docs/messages.html) and click on "Try it" button then we can set value for GlobalMergeVars but I think there is some problem in the package I am using. But I can't switch to other package because it is very flexible as compare to others. So is there anyway to set value for GlobalMergeVars?

PS: I have also opened an issue on github repo. But due to some close deadlines I need a quick fix.


Solution

  • I think I've found the class you're talking about here. Although that property of EmailMessage is given differently than you show in the question:

    public class EmailMessage
    {
        #region Public Properties
        ...
        /// <summary>
        ///   Gets the global_merge_vars.
        /// </summary>
        public List<MergeVar> GlobalMergeVars { get; private set; }
    

    (The differences between the code here and the code given in the question are immaterial - I'm just wondering whether I'm looking at the right file!)

    The missing set is the equivalent of private set. Within that class is the following method, which will allow you to set the variables:

    public void AddGlobalVariable(string name, dynamic content)
    {
      if (GlobalMergeVars == null)
      {
        GlobalMergeVars = new List<MergeVar>();
      }
    
      var mv = new MergeVar {Name = name, Content = content};
      GlobalMergeVars.Add(mv);
    }