I have a method
private static Dictionary<string, string> getRelationPropertyAttribute(Type type)
{
var dicRelation = new Dictionary<string, string>();
var properties = type.GetProperties();
foreach (var property in properties)
{
var attributes = property.GetCustomAttributes(inherit: false);
var customAttributes = attributes
.AsEnumerable()
.Where(a => a.GetType() == typeof(MongoDBFieldAttribute));
if (customAttributes.Count() <= 0)
continue;
for each (var attribute in custom attributes)
{
if (attribute is MongoDBFieldAttribute attr)
dicRelation[attr.Field] = property.Name;
}
}
return dicRelation;
}
In this typeof(MongoDBFieldAttribute) is getting me CustomAttributes list of all properties with MOngoDBFieldAttribute type only and I have properties as :
[FieldIdentifier("SSI")]
[MongoDBField("Sender State ID")]
public string SenderStateID { get; set; }
[FieldIdentifier("SPH")]
[MongoDBField("Sender Phone")]
public string SenderPhone { get; set; }
How can I make the method generic so as to get Dictionary of MongoDBField or FieldIdentifier based on need?