This repository has been archived on 2024-03-05. You can view files and clone it, but cannot push or open issues or pull requests.
Salesforce.NET/PropertyExtensions.cs
2021-04-20 18:10:07 -04:00

66 lines
2.0 KiB
C#

using BinaryDad.Extensions;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Salesforce.NET
{
internal static class PropertyExtensions
{
internal static IEnumerable<PropertyInfo> WhereIsSerializable(this IEnumerable<PropertyInfo> properties)
{
foreach (var property in properties)
{
var jsonIgnore = property.GetCustomAttribute<JsonIgnoreAttribute>();
if (jsonIgnore == null)
{
yield return property;
}
}
}
internal static IEnumerable<PropertyInfo> WhereIsQueryable(this IEnumerable<PropertyInfo> properties)
{
foreach (var property in properties)
{
var queryIgnore = property.GetCustomAttribute<QueryIgnoreAttribute>();
if (queryIgnore == null)
{
yield return property;
}
}
}
internal static IDictionary<string, object> GetPropertyValues(this IEnumerable<PropertyInfo> properties, object value)
{
return properties
.Select(p =>
{
var propertyName = GetSalesforcePropertyName(p);
var propertyValue = p.GetValue(value);
return new KeyValuePair<string, object>(propertyName, propertyValue);
})
.ToDictionary();
}
internal static IEnumerable<string> GetPropertyNames(this IEnumerable<PropertyInfo> properties)
{
foreach (var property in properties)
{
yield return GetSalesforcePropertyName(property);
}
}
private static string GetSalesforcePropertyName(PropertyInfo propertyInfo)
{
var jsonProperty = propertyInfo.GetCustomAttribute<JsonPropertyAttribute>();
return jsonProperty?.PropertyName ?? propertyInfo.Name;
}
}
}