Is there a clean way to query Type for properties and filter those that came from an interface?
Let's say I have a class and an interface
public interface IFoo
{
string Bar { get; set; }
}
public class Foo : IFoo
{
public string Bar { get; set; }
public string Baz { get; set; }
}
And I want to get an array of PropertyInfo's that contain only Baz property.
Edit:
This is what I have now ... I know it's not perfect, but it kinda does the job.
var allProperties = typeof(T).GetProperties();
var interfaceMethods = typeof(T).GetInterfaceMap(typeof(IFoo)).TargetMethods;
return allProperties.Where(x => !interfaceMethods.Contains(x.GetGetMethod()) || !interfaceMethods.Contains(x.GetSetMethod())).ToArray();
Looks like you want to use InterfaceMapping:
private static bool IsInterfaceImplementation(PropertyInfo p, InterfaceMapping interfaceMap)
{
var getterIndex = Array.IndexOf(interfaceMap.TargetMethods, p.GetGetMethod());
var setterIndex = Array.IndexOf(interfaceMap.TargetMethods, p.GetSetMethod());
return getterIndex != -1 || setterIndex != -1;
}
private static PropertyInfo[] GetPropertiesExcludeInterfaceImplementation(Type type, Type interfaceType)
{
var interfaceMap = type.GetInterfaceMap(interfaceType);
return type
.GetProperties()
.Where(p => !IsInterfaceImplementation(p, interfaceMap))
.ToArray();
}
You could load all interfaces that are implemented by your class and get all the properties of these interfaces. When getting the properties of your class you can check whether the property is already defined by one of the interfaces:
var interfaceProperties = typeof(Foo)
.GetInterfaces()
.SelectMany( i => i.GetProperties() )
.ToList();
var properties = typeof(Foo)
.GetProperties()
.Where(p => !interfaceProperties.Any( ip => ip.Name==p.Name && ip.PropertyType==p.PropertyType) );
Related
I need to filter objects of a container base on the Interface they implement, one of them is Ibase, this will do it for known interfaces:
selObjs = container.Where(component => typeof(Ibase).IsAssignableFrom(component.GetType())).ToList();
But I get the interface names from a config file so as the Interface can't be instantiated, I'm unable to use a variable attached to GetType(), additionally the use of a switch is not practical, so I'm getting the Interface type from the Assembly.
string interfaceName = "IbaseG<Type>";
interfaceName = $"{interfaceName .Substring(0, interfaceName.IndexOf("<"))}`1";
Type selInterf = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(t => t.GetTypes())
.Where(t => String.Equals(t.Name, interfaceName, StringComparison.Ordinal)).ToArray()[0];
selObjs = Container.Where(component => selInterf.IsAssignableFrom(component.GetType())).ToList();
It works great for non-generic interfaces, but if the Interface is Generic, The Interface retrieved from the Assembly has the same name and GUID of the typeof(IbaseG<Type>) but the Type and FullName are different, and it doesn't match using IsAssignableFrom.
This is a small Console App that show the differences between Generic and Non-Generic Interfaces
namespace ConsoleApp {
using System;
using System.Collections.Generic;
using System.Linq;
public interface IbaseG<T> { T Val { get; } }
public class BaseG<T> : IbaseG<T> {
public T Val { get; }
public BaseG() { }
}
public class ExtG<T> : BaseG<T> {
public ExtG() { }
}
public interface Ibase { string Val { get; } }
public class Base : Ibase {
public string Val { get; }
public Base() { }
}
public class Ext : Base {
public Ext() { }
}
class Program {
static void Main(string[] args) {
Console.WriteLine("Begin Test");
bool isAssignable = typeof(IbaseG<string>)
.IsAssignableFrom(new ExtG<string>().GetType()); // true
// Non Generic Type
Test("Ibase", typeof(Ibase), new Ext());
// Generic Type
Test("IbaseG<string>", typeof(IbaseG<string>), new ExtG<string>());
Console.ReadLine();
}
static void Test(string interName, Type typeofType, object obj) {
if (typeofType.IsGenericType)
interName = $"{interName.Substring(0, interName.IndexOf("<"))}`1";
Type assmType = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(t => t.GetTypes())
.Where(t => String.Equals(t.Name, interName, StringComparison.Ordinal))
.ToArray()[0];
Dictionary<string, string> results = new Dictionary<string, string>();
results.Add($"assembly.GetType({assmType.Name})
.IsAssignableFrom({obj.GetType().Name}) 1",
(assmType.IsAssignableFrom(obj.GetType()).ToString()));
results.Add($"typeof({typeofType.Name})
.IsAssignableFrom({obj.GetType().Name}) 2",
(typeofType.IsAssignableFrom(obj.GetType()).ToString()));
results.Add("Same Type", (assmType == typeofType).ToString()); // true
results.Add("Same Name", (assmType.Name == typeofType.Name).ToString()); // true
results.Add("Same FullName", (assmType.FullName == typeofType.FullName).ToString()); // true
results.Add("Same GUID", (assmType.GUID == typeofType.GUID).ToString()); // true
if (assmType.IsGenericType)
Console.WriteLine($"Generic Type: {interName} Results");
else
Console.WriteLine($"Non Generic Type: {interName} Results");
foreach (KeyValuePair<string, string> result in results)
Console.WriteLine($"{result.Key}: {result.Value}");
Console.WriteLine($"typeof({typeofType.Name}).FullName: {typeofType.FullName}");
Console.WriteLine($"assembly.GetType({assmType.Name}).FullName: {assmType.FullName}");
Console.WriteLine($"typeof({typeofType.Name}).GUID: {typeofType.GUID}");
Console.WriteLine($"assembly.GetType({assmType.Name}).GUID: {assmType.GUID}");
Console.WriteLine();
}
}
}
Any help will be greatly appreciated.
based of #Selvin comment.
The Interface retrieved from the assembly has to be MakeGenericType of the same type as the object:
// ExtG<Type> implements the intreface IbaseG<Type>
string interName = "IbaseG<Type>";
Type typeofType = typeof(IbaseG<Type>);
object obj = new ExtG<Type>();
if (typeofType.IsGenericType)
interName = $"{interName.Substring(0, interName.IndexOf("<"))}`1";
Type assmType = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(t => t.GetTypes())
.Where(t => String.Equals(t.Name, interName, StringComparison.Ordinal))
.ToArray()[0];
if (typeofType.IsGenericType)
assmType = assmType.MakeGenericType(typeof(Type));
bool test1 = typeof(IbaseG<Type>).IsAssignableFrom(obj.GetType()); //true
bool test2 = (assmType.IsAssignableFrom(obj.GetType())); // true
What is the correct way to check that a given property info is an implementation of a property from an interface?
There is a class InterfaceMap that solve this problem for methods. But for properties, it provides two separate mappings for getter and setter and there still remains a problem to match those with the corresponding interface methods.
public interface IA
{
int X { get; set; }
}
public interface IB
{
int X { get; set; }
}
public class C : IA, IB
{
public int X { get; set; }
int IB.X { get; set; }
}
public PropertyInfo GetProperty<TClass, TProperty>(Expression<Func<TClass, TProperty>> getProperty)
{
return (PropertyInfo)((MemberExpression)getProperty.Body).Member;
}
[Test]
public void Check()
{
var aProperty = GetProperty((IA x) => x.X);
var bProperty = GetProperty((IB x) => x.X);
var cPropertyA = GetProperty((C x) => x.X);
var cPropertyB = GetProperty((C x) => ((IB)x).X);
CompareProperties(cPropertyA, aProperty); // True
CompareProperties(cPropertyA, bProperty); // False
CompareProperties(cPropertyB, aProperty); // False
CompareProperties(cPropertyB, bProperty); // True
}
private bool CompareProperties(PropertyInfo classProperty, PropertyInfo interfaceProperty)
{
// TODO implement
}
From a given PropertyInfo, you can use the GetMethod and SetMethod properties to access the MethodInfo of the getter and setter, respectively.
Thus, it should be possible to compare those in a little helper method:
private static bool MethodsImplements(InterfaceMap interfaceMap,
MethodInfo interfaceMethod, MethodInfo classMethod)
{
var implIndex = Array.IndexOf(interfaceMap.InterfaceMethods, interfaceMethod);
return interfaceMethod == interfaceMap.TargetMethods[implIndex];
}
This can then be used as follows to fulfil your desired method:
var interfaceType = interfaceProperty.DeclaringType;
var interfaceMap = classProperty.DeclaringType.GetInterfaceMap(interfaceType);
var gettersMatch = classProperty.CanRead && interfaceProperty.CanRead
&& MethodImplements(interfaceMap, interfaceProperty.GetMethod, classProperty.GetMethod);
var settersMatch = classProperty.CanWrite && interfaceProperty.CanWrite
&& MethodImplements(interfaceMap, interfaceProperty.SetMethod, classProperty.SetMethod);
Then, return gettersMatch || settersMatch, as the interface property may have only a getter or only a setter.
How do I make this expression dynamic based on the generic type passed in the parameter?
In the simplified form:
public static class CompareService
{
public static List<T> Run<T>(List<T> database_list, string directory_path)
{
var csv_list = CompareService.MergeRecordsFromFiles<T>(directory);
return CompareService.RunComparison<T>(database_list, csv_list);
}
public static T CompareData<T>(List<T> database_list, List<T> csv_list)
{
var diff = new List<T>();
foreach (var db_item in database_list)
{
// ...
// if T is of type Deathstar compare reference_number property
// if T is of type Stormtrooper compare id property
// if T is of type Sith compare id and anger_level property
var csv_item = csv_list.FirstOrDefault(x => x.reference_number == db_item.reference_number);
// Comparison code
ComparisonResult result = compareLogic.Compare(db_item, csv_item);
// ...
}
return diff;
}
}
It is called from another generic service:
public static void Whatever<T>(List<T> list)
{
// ...
var directory_path = "C:\";
var delta = CompareService.CompareData<T>(list, directory_path);
// ...
}
The most naive implementation would be to check if your itemToFind can be cast to DeathStar, StormTrooper or Sith and if so call the instances property.
var deathStar = itemToFind as DeathStar;
if(deathStar != null)
return database_list.Where(x => ((DeathStar)x).reference_number == deathStar.reference_number).FirstOrDefault();
else
{
var sith = itemToFind as Sith;
if(sith != null)
return database_list.Where(x => ((Sith)x).anger_level == sith.anger_level).FirstOrDefault();
else
return database_list.Where(x => ((StormTrooper)x).id== ((StormTrooper)item).id).FirstOrDefault();
}
This is quite cumbersome, including many casts. In particular it completely bypasses the actual benefits of generics using any arbitrary type (that fullfills the constraints if existing). In your case you´d have a generic method that will only wortk for three decent types.
A better approach is to let all your classes implement a common interface that defines a property, for instance:
interface IObject {
int Level { get; }
}
Now all classes define that level-property:
clas DeathStar : IObject
{
public int Level { get { return this.reference_number; } }
}
clas Sith : IObject
{
public int Level { get { return this.anger_level; } }
}
clas StormTrooper: IObject
{
public int Level { get { return this.id; } }
}
Than you can use a constraint on your type T to implement that interface:
public static T CompareData<T>(List<T> list, T itemToFind) where T: IObject
Why not like this:
public static T CompareData<T>(List<T> list, Func<T, bool> predicate)
{
return database_list.FirstOrDefault(predicate);
}
And then use it like this:
var itemToFind = new ItemToFind();
var myObjectList = new List<MyObject>();
var item = CompareData<MyObject>(myObjectList, x=> x.MyObjectProperty == itemToFind.Id);
You could add a property selector:
public static class CompareService
{
public static T CompareData<T>(this List<T> list, T itemToFind, Func<T, int> propSelector)
{
int propToFind = propSelector(itemToFind); // cache
return database_list.FirstOrDefault(x => propSelector(x) == propToFind);
}
}
And call it like that:
listOfDeathstars.CompareData(deathStarToFind, ds => ds.reference_number);
listOfStormtroopers.CompareData(trooperToFind, t => t.id);
listOfSiths.CompareData(sithStarToFind, sith => new { sith.id, sith.anger_level});
Note: I added the this keyword in the signature to make it an extension (not sure if you intended that but forgot the keyword). And Where(predicate).FirstOrDefault() can be reduced to FirstOrDefault(predicate).
I have two very simple objects:
public class CategoryDto
{
public string Id { get; set; }
public string MyValueProperty { get; set; }
}
public class Category
{
public string Id { get; set; }
[MapTo("MyValueProperty")]
public string Key { get; set; }
}
When mapping a Category to a CategoryDto with AutoMapper, I would like the following behavior:
The properties should be mapped as usual, except for those that have the MapTo attribute. In this case, I have to read the value of the Attribute to find the target property. The value of the source property is used to find the value to inject in the destination property (with the help of a dictionary). An example is always better that 1000 words...
Example:
Dictionary<string, string> keys =
new Dictionary<string, string> { { "MyKey", "MyValue" } };
Category category = new Category();
category.Id = "3";
category.Key = "MyKey";
CategoryDto result = Map<Category, CategoryDto>(category);
result.Id // Expected : "3"
result.MyValueProperty // Expected : "MyValue"
The Key property is mapped to the MyValueProperty (via the MapTo Attribute), and the assigned value is "MyValue", because the source property value is "MyKey" which is mapped (via dictionary) to "MyValue".
Is this possible using AutoMapper ? I need of course a solution that works on every object, not just on Category/CategoryDto.
I finally (after so many hours !!!!) found a solution.
I share this with the community; hopefully it will help someone else...
Edit: Note that it's now much simpler (AutoMapper 5.0+), you can do like I answered in this post: How to make AutoMapper truncate strings according to MaxLength attribute?
public static class Extensions
{
public static IMappingExpression<TSource, TDestination> MapTo<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression)
{
Type sourceType = typeof(TSource);
Type destinationType = typeof(TDestination);
TypeMap existingMaps = Mapper.GetAllTypeMaps().First(b => b.SourceType == sourceType && b.DestinationType == destinationType);
string[] missingMappings = existingMaps.GetUnmappedPropertyNames();
if (missingMappings.Any())
{
PropertyInfo[] sourceProperties = sourceType.GetProperties();
foreach (string property in missingMappings)
{
foreach (PropertyInfo propertyInfo in sourceProperties)
{
MapToAttribute attr = propertyInfo.GetCustomAttribute<MapToAttribute>();
if (attr != null && attr.Name == property)
{
expression.ForMember(property, opt => opt.ResolveUsing(new MyValueResolve(propertyInfo)));
}
}
}
}
return expression;
}
}
public class MyValueResolve : IValueResolver
{
private readonly PropertyInfo pInfo = null;
public MyValueResolve(PropertyInfo pInfo)
{
this.pInfo = pInfo;
}
public ResolutionResult Resolve(ResolutionResult source)
{
string key = pInfo.GetValue(source.Value) as string;
string value = dictonary[key];
return source.New(value);
}
}
This should be fairly straightforward using an implementation of IValueResolver and the ResolveUsing() method. You basically just need to have a constructor for the resolver that takes in the property info (or if you wanna be fancy that takes in a lambda expression and resolves the property info similar to How to get the PropertyInfo of a specific property?. Though I haven't tested it myself I imagine the following would work:
public class PropertyBasedResolver : IValueResolver
{
public PropertyInfo Property { get; set; }
public PropertyBasedResolver(PropertyInfo property)
{
this.Property = property;
}
public ResolutionResult Resolve(ResolutionResult source)
{
var result = GetValueFromKey(property, source.Value); // gets from some static cache or dictionary elsewhere in your code by reading the prop info and then using that to look up the value based on the key as appropriate
return source.New(result)
}
}
Then to set up that mapping you need to do:
AutoMapper.Mapper.CreateMap<Category, CategoryDto>()
.ForMember(
dest => dest.Value,
opt => opt.ResolveUsing(
src =>
new PropertyBasedResolver(typeof(Category.Key) as PropertyInfo).Resolve(src)));
Of course that is a pretty gross lamda and I would suggest that you clean it up by having your property resolver determine the property on the source object it should look at based on the attribute/property info so you can just pass a clean new PropertyBasedResolver(property) into the ResolveUsing() but hopefully this explains enough to put you on the right track.
Lets assume I have the following classes
public class foo
{
public string Value;
}
public class bar
{
public string Value1;
public string Value2;
}
You can pass a lambda to ResolveUsing:
.ForMember(f => f.Value, o => o.ResolveUsing(b =>
{
if (b.Value1.StartsWith("A"));)
{
return b.Value1;
}
return b.Value2;
}
));
Is there a way to convertFunc<IQueryable<TD>, IOrderedQueryable<TD>> to Func<IQueryable<TE>, IOrderedQueryable<TE>>.
Let's say I have classes Country and CountryModel.
class CountryModel
{
public string Name {get;set;}
}
class Country{
public string Name{get;set;}
}
class Repo{
public IEnumerable<TD> Get(
Func<IQueryable<TD>, IOrderedQueryable<TD>> orderBy = null)
{
IQueryable<TEntityModel> query = CurrentDbSet;
return orderBy(query).ToList(); // Convert orderBy to TE.
}
}
Using the above method I would pass CountryModel instance. But the query has to happen the entity type Country.
There might be some syntax errors. Apologies for that.
public class Repo
{
public IEnumerable<TD> Get<TD, TE>(Func<IQueryable<TD>, IOrderedQueryable<TD>> orderBy = null)
{
IQueryable<TD> query = new List<TE>().Select(t => Convert<TD, TE>(t)).AsQueryable();
return orderBy(query).AsEnumerable(); // Convert orderBy to TE.
}
public TD Convert<TD, TE>(TE input) where TD : class
{
return input as TD;
}
}
If your type TE can be casted to the type TD this should work.
You can define explicit or implicit operator in your CountryModel to convert from Country
You may find AutoMapper to be very useful.
public static class MappingExtensions
{
static MappingExtensions()
{
Mapper.CreateMap<CustomAlerts, Domain.Models.CustomAlerts>();
Mapper.CreateMap<Domain.Models.CustomAlerts, CustomAlerts>();
//add mappings for each set of objects here.
//Remember to map both ways(from x to y and from y to x)
}
//map single objects
public static TDestination Map<TSource, TDestination>(TSource item)
{
return Mapper.Map<TSource, TDestination>(item);
}
//map collections
public static IEnumerable<TDestination> Map<TSource, TDestination>(IEnumerable<TSource> item)
{
return Mapper.Map<IEnumerable<TSource>, IEnumerable<TDestination>>(item);
}
}
Then in my code, I can do the following:
var TodayDate = DateTime.Now.AddDays(1);
var Alerts = DB.CustomAlerts
.Where(x => x.EndDate >= TodayDate)
.OrderByDescending(x => x.DateCreated)
.Skip(((id - 1) * 50))
.Take(50);
return Mapping.MappingExtensions.Map<CustomAlert,Models.CustomAlert>(Alerts).ToList();
Using AutoMapper and wrapping your Func with Expression should help you:
// Initialize AutoMapper
Mapper.Initialize(cfg =>
{
cfg.CreateMap<TE, TD>();
cfg.CreateMap<TD, TE>();
});
public class Repo
{
// Wrap Func with Expression
public IEnumerable<TD> Get(Expression<Func<IQueryable<TD>, IOrderedQueryable<TD>>> orderBy = null)
{
var orderByExpr = Mapper.Map<Expression<Func<IQueryable<TE>, IOrderedQueryable<TE>>>>(orderBy);
IQueryable<TE> query = CurrentDbSet;
// Compile expression and execute as function
var items = orderByExpr.Compile()(query).ToList();
// Map back to list of TD
return Mapper.Map<IEnumerable<TD>>(items);
}
}