hubspot-net / HubSpot.NET

C# .NET Wrapper around the common HubSpot APIs.
MIT License
112 stars 137 forks source link

Using custom properties dynamically #111

Open jancisl1 opened 3 years ago

jancisl1 commented 3 years ago

Hi, I'm new to github community, I don't how these things should be properly addresed, but I really needed to use custom properties dynamically for contacts, but didn't know how, so I searched through source code and found in PropertyTransport class, that there was a code for this but it's buggy.

In "\Api\Shared\PropertyTransportModel.cs", there is this method "ToPropertyTransportModel" which is used to add properties to request body

Starting from line 35

    else if(typeof(IEnumerable<>).IsAssignableFrom(prop.PropertyType) && typeof(PropertyValuePair).IsAssignableFrom(prop.PropertyType.GetElementType()))             
               {
                   IEnumerable<PropertyValuePair> pairs = value as IEnumerable<PropertyValuePair>;
                   foreach (var item in pairs)
                   {
                       Properties.Add(item);
                   }
                    continue;
              }

So if you are extending ContactHubSpotModel with this kind of property, you should be able to put there you're custom properties

[DataMember(Name ="CustomProperties")]
[IgnoreDataMember]
public List<PropertyValuePair> CustomProperties { get; set; } = new List<PropertyValuePair>();

But it doesn't work, because of the buggy if-statement. When I changed it to this, it it worked.

 else if(
              (
                   typeof(IEnumerable<>).IsAssignableFrom(prop.PropertyType) 
                   && typeof(PropertyValuePair).IsAssignableFrom(prop.PropertyType.GetElementType())
              )
             || typeof(IEnumerable<PropertyValuePair>).IsAssignableFrom(prop.PropertyType)
       )

Do I understand correctly how this should be used? Is there a better way to user properties dynamically? Maybe this fix needs to be implemented?