I have created an extension method as per an answer on SO here
public class AcObject
{
public int Id { get; set; }
}
public static Dictionary<string, string> GetValidationList<AcObject, TProperty>(
this AcObject source,
Expression<Func<AcObject, TProperty>> propertyLambda)
{
// Autocomplete here only shows static members for 'source'
// I am expecting to be able to do source.Id
}
Anyone able to explain to me why I can't use source.Id
in the above scenario and suggest where I can look to come up with a similar solution?
If I set a breakpoint inside the GetValidationList()
method I can mouseover source and see the instance and it's properties as one would expect... I just can't use it in VS.
My overall goal is to be able to do the following
public class AcObject
{
public int Id { get; set; }
public string Type { get; set; }
}
public class OtherObject : AcObject
{
public string AssetTag { get; set; }
}
// somewhere else in code
AcObject myObject = new AcObject();
myObject.GetValidationList(a => a.Type);
// Along with using the type that inherits it
OtherObject myOtherObject = new OtherObject();
myOtherObject.GetValidationList(a => a.Type);
// In some kind of extension method lambda magic
{
Console.WriteLine(source.Id);
}
Edit - Updated to include the requirement of it working on base classes as well as those that inherit it.