Handling generic properties via reflection - c#

If I have the following wrapper class:
public class Wrapper<T>
{
public T Data { get; set; }
public string[] Metadata { get;set;
}
and another class then exposes that value without generics:
public class SomeOtherClass
{
public object WrappedData { get;set };
}
, how can I get at the original unwrapped data?
I can test for it, using something like:
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Wrapper<>))
{
dynamic originalValue = someOtherClass.WrappedData;
}
but I can't then call the .Data property on originalValue, getting a RuntimeBinderException.
Update
A little more context might help. I am working on a WebAPI where I am wanting to implement HATEOAS. So my wrapper class is containing the data that will be returned plus metadata, and I am writing an action filter that will unwrap the data, returning it in the response body, and put the metadata into response headers. The action filter is currently implemented as follows:
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if (actionExecutedContext.Request.Method == HttpMethod.Get)
{
var objectContent = actionExecutedContext.Response.Content as ObjectContent;
if (objectContent != null)
{
var type = objectContent.ObjectType;
var formatter = actionExecutedContext
.ActionContext
.ControllerContext
.Configuration
.Formatters
.First(f => f.SupportedMediaTypes
.Contains(new MediaTypeHeaderValue(actionExecutedContext
.Response
.Content
.Headers
.ContentType
.MediaType)));
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Wrapper<>))
{
dynamic value = objectContent.Value;
actionExecutedContext.Response.Content = new ObjectContent(value.Data.GetType(), value.Data, formatter);
}
}
}
base.OnActionExecuted(actionExecutedContext);
}
Obviously not all my API endpoints currently wrap their data, so if the response is not returning a Wrapper<T> instance, I want to exit the action filter without modifying the response. If it is, then pull out the value of .Data and rewrite the response body with it.

It's not clear from the posted code what objectContent.ObjectType is, so I would modify the code to check the actual value:
object value = objectContent.Value;
if (value != null && value.GetType().IsGenericType && value.GetType().GetGenericTypeDefinition() == typeof(Wrapper<>))
{
object data = ((dynamic)value).Data;
actionExecutedContext.Response.Content = new ObjectContent(data.GetType(), data, formatter);
}
But you can avoid reflection and dynamic calls and make your life much easier if you backup your generic class with a non generic interface. For instance
public interface IWrapper
{
object Data { get; }
string[] Metadata { get; }
}
public class Wrapper<T> : IWrapper
{
public T Data { get; set; }
object IWrapper.Data { get { return Data; } }
public string[] Metadata { get; set; }
}
Then you can do simple
var wrapper = objectContent.Value as IWrapper;
if (wrapper != null)
{
actionExecutedContext.Response.Content = new ObjectContent(wrapper.Data.GetType(), wrapper.Data, formatter);
}

The following code works:
using System;
namespace ConsoleApplication1
{
class Program
{
public class Wrapper<T>
{
public T Data { get; set; }
public string[] Metadata
{
get; set;
}
}
public class SomeOtherClass
{
public object WrappedData { get; set; }
}
static void Main(string[] args)
{
var wrappedData = new Wrapper<int> { Data = 3 };
var someObject = new SomeOtherClass { WrappedData = wrappedData };
dynamic d = someObject.WrappedData;
Console.WriteLine(d.Data);
}
}
}
So, It isn't clear what your problem is!

Would it help if you added the type to your wrapper?
public class Wrapper<T>
{
public Type MyType{get;set;}
public T Data { get; set; }
public string[] Metadata { get;set;}
public Wrapper(T data){
MyType = data.GetType();
Data = data;
}
}

Unclear exactly what you are trying to do:
object obj = new Wrapper<SomeOtherClass> { Data = new SomeOtherClass { WrappedData = "Hello" } };
Type type = obj.GetType();
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Wrapper<>))
{
dynamic data = ((dynamic)obj).Data;
dynamic wrappedData = data.WrappedData;
}
Note that there is no guarantee that there is a WrappedData property inside the Data property: WrappedData is a property of SomeOtherClass, but for example obj could be:
object obj = new Wrapper<string> { Data = "Hello" };

Related

How to get a specific constructor that matches an Interface?

So playing with my own test Dependency Injector class. (yeah tons out there but this is just for fun)
Works decent but I don't know how to get the correct constructor based on the Interface passed in.
internal class DiContainer
{
private readonly Dictionary<Type, RegistryRecord> registry = new Dictionary<Type, RegistryRecord>();
private static DiContainer instance;
private DiContainer()
{
}
public static DiContainer GetInstance()
{
return instance ??= new DiContainer();
}
public void Register<T, C>() where C : class, T
{
registry.Add(typeof(T), new RegistryRecord
{
InterfaceType = typeof(T),
ConcreteType = typeof(C),
IsSingleTon = false
});
}
public void Register<C>() where C : class
{
Register(typeof(C));
}
public void Register(Type t)
{
registry.Add(t, new RegistryRecord
{
InterfaceType = t,
ConcreteType = t,
IsSingleTon = false
});
}
public void RegisterSingleton<T, C>(C instance = null) where C : class, T
{
registry.Add(typeof(T), new RegistryRecord
{
InterfaceType = typeof(T),
ConcreteType = typeof(C),
IsSingleTon = true,
Instance = instance
});
}
public T Get<T>()
{
return (T) Get(typeof(T));
}
public object Get(Type t)
{
ConstructorInfo constructor;
RegistryRecord r = null;
if (t.IsInterface && registry.ContainsKey(t))
{
r = registry[t];
if (r.IsSingleTon && r.Instance != null) return r.Instance;
constructor = r.ConcreteType.GetConstructors(BindingFlags.Instance | BindingFlags.Public)[0];
}
else
{
//todo how do we select the correct constructor?
constructor = t.GetConstructors(BindingFlags.Instance | BindingFlags.Public)[0];
}
var parameters = constructor.GetParameters();
//recurse to build dependency chain
var objects = parameters.Select(parameter => Get(parameter.ParameterType)).ToList();
var obj = constructor.Invoke(objects.ToArray());
if (r != null && r.IsSingleTon)
{
r.Instance = obj;
}
return obj;
}
}
internal class RegistryRecord
{
public Type InterfaceType { get; set; }
public Type ConcreteType { get; set; }
public object Instance { get; set; }
public bool IsSingleTon { get; set; }
}
So the problem is
constructor = t.GetConstructors(BindingFlags.Instance | BindingFlags.Public)[0];
I am just assuming the first constructor which is awful. But I have the definition of the interface I could be using.
How do I get the parameters of my interface and check them against the constructor?
Would like to select a constructor that matches the interface, or at least partially matches optimally.
edit
An example
using System;
namespace DITest
{
internal class Program
{
private static void Main(string[] args)
{
var di = DiContainer.GetInstance();
//Register classes / interfaces
di.Register<IPhoneResolver, PhoneResolver>();
di.Register<Customer>();
//Get class where dependency should be injected
var x = di.Get<Customer>();
Console.WriteLine(x.resolver.Name);
Console.Read();
}
}
public class Customer
{
//Remove this and everything is ok. Because we select the first one not the right one
public Customer()
{
}
public Customer(IPhoneResolver resolver)
{
this.resolver = resolver;
}
public IPhoneResolver resolver { get; set; }
}
public interface IPhoneResolver
{
string Name { get; set; }
bool DoesSomething();
}
public class PhoneResolver : IPhoneResolver
{
public string Name { get; set; } = "test";
public bool DoesSomething()
{
return true;
}
}
}
So because the first constructor is null there is an issue.
I need a way to resolve the correct constructor. I have the interface via the RegistryRecord and (type) InterfaceType. I need to find a way to get a constructor that matches that types parameters.

Cast list item to generic type at runtime

I have a following interface:
interface IStorage
{ }
and then I have a class that derives from this interface (which is generic) with, for example, with a property Get
public class ManagingModel<T> : IStorage
{
public Func<T> Get { get; set; }
}
To have a list of those objects, I'm using the List<IStorage>
The question is, how do I cast the item from the List to ManagingModel<> to access this property?
For example you can access list element by index. Example:
var managingString = new ManagingModel<string>();
var managingInt = new ManagingModel<int>();
var managingDouble = new ManagingModel<double>();
var list = new List<IStorage>();
list.Add(managingString);
list.Add(managingInt);
list.Add(managingDouble);
Trying to cast "as" given model via index:
var backToManagingModel = list[1] as ManagingModel<int>;
if (backToManagingModel != null)
{
var get = backToManagingModel.Get;
}
If backToManagingModel is null after casting, then it's being casted to wrong type, otherwise casting is sucessful and you can get your property.
Edit: What about not using generics at all, but simply use object?
public static string GetString()
{
return "xyz";
}
public interface IStorage
{
Func<object> Get { get; set; }
}
public class ManagingModel : IStorage
{
public Func<object> Get { get; set; }
}
You won't need to check all the types, just call list[index].Get
var managingString = new ManagingModel
{
Get = new Func<string>(GetString)
};
var list = new List<IStorage>();
list.Add(managingString);
var get = list[1].Get;
Since the value of the Func<T> is going to a view then you probably can extend IStorage to do it as a Func<object>.
Try this:
interface IStorage
{
Func<object> Get { get; }
}
public class ManagingModel<T> : IStorage
{
public Func<T> Get { get; set; }
Func<object> IStorage.Get
{
get
{
return () => this.Get();
}
}
}

Add to a collection of unknown type using reflection in c#

So I am using reflection to loop through the properties of one object and populating the values on a different object with properties of the same name. This works great but the problem comes when the property type is a collection. I want to be able to loop through each of the objects in the source collection and populate the same list with objects in the source collection.
public class SourceMessage
{
public string Name { get; set; }
public int Version { get; set; }
public IList<ValueDefinition> Values { get; set; }
}
public class ValueDefinition
{
public string Name { get; set; }
public string Value { get; set; }
}
public class TargetObject
{
public TargetObject()
{
Values = new List<TargetValueDefinition>();
}
public string Name { get; set; }
public int Version { get; set; }
public IList<TargetValueDefinition> Values { get; set; }
}
public class TargetValueDefinition
{
public string Name { get; set; }
public string Value { get; set; }
}
Then I use Reflection to populate the target from the source.
public static void PopulateFromMessage<T, TS>(ref T targetEntity, TS message)
{
var sourceType = typeof(TS);
var targetType = typeof(T);
foreach (var targetPropInfo in targetType.GetProperties())
{
if (sourceType.GetProperty(targetPropInfo.Name) != null)
{
var obj = sourceType.GetProperty(targetPropInfo.Name);
if (obj.PropertyType.Namespace == "System.Collections.Generic")
{
//var x = targetType.GetProperty(targetPropInfo.Name);
//PopulateFromMessage(ref x, sourceType.GetProperty(targetPropInfo.Name));
continue;
}
targetPropInfo.SetValue(targetEntity, sourceType.GetProperty(targetPropInfo.Name).GetValue(message), null);
}
}
}
So calling this would be like this:
private void DenormalizeMessage(SourceMessage message)
{
var newTargetObject = new TargetObject();
PopulateFromMessage(ref newTargetObject , message);
}
I can identify when the property is a collection but am uncertain of how to create new TargetValueDefinitions and populate them with the values from ValueDefinitions. In the end it is pretty much a copy of the SourceMessage in the form of a TargetObject.
This all stems from receiving messages and transforming them into objects with the same property names.
If your problem is iterating through items contained inside a single property when it is a collection, then the key would be to read the property value into a dynamic variable and not an object variable that is by default, this way you could use a foreach for it.
dynamic propVal = inputProperty.GetValue(item);
foreach (var subItem in propVal)
{
//do your stuff
}
Disclaimer: This is extremely unsafe to do and makes a lot of assumptions but it should puth you on the right path.
Change you method to this:
public static void PopulateFromMessage<T, TS>(T targetEntity, TS message)
{
var sourceType = typeof (TS);
var targetType = typeof (T);
foreach (var targetPropInfo in targetType.GetProperties())
{
if (targetPropInfo.PropertyType.IsGenericType)
{
if (targetPropInfo.PropertyType.GetGenericTypeDefinition() == typeof(IList<>))
{
var originalList = sourceType.GetProperty(targetPropInfo.Name).GetValue(message) as IList;
if (originalList != null)
{
var argumentType = targetPropInfo.PropertyType.GetGenericArguments();
var listType = typeof (List<>);
var concreteType = listType.MakeGenericType(argumentType);
var newList = Activator.CreateInstance(concreteType) as IList;
foreach (var original in originalList)
{
var targetValue = Activator.CreateInstance(argumentType[0]);
// do this yourself. Here we're converting ValueDefinition to TargetValueDefinition
// targetValue.Fill(original);
}
targetPropInfo.SetValue(targetEntity, newList);
}
}
}
else
{
if (sourceType.GetProperty(targetPropInfo.Name) != null)
{
var obj = sourceType.GetProperty(targetPropInfo.Name);
if (obj.PropertyType.Namespace == "System.Collections.Generic")
{
//var x = targetType.GetProperty(targetPropInfo.Name);
//PopulateFromMessage(ref x, sourceType.GetProperty(targetPropInfo.Name));
continue;
}
targetPropInfo.SetValue(targetEntity, sourceType.GetProperty(targetPropInfo.Name).GetValue(message), null);
}
}
}
}
You should create a interface for each class (implement the methods and properties on interface) and implement it in each class. After, in function PopulateFromMessage should specify the interface allowed in method, with this you can use directly the properties of class with T and TS generic types.

Get custom attribute from specific object property/field

I've been searching for a while now and tested several methods, but i didn't find the answer i was looking for. I'll try to explain.
I have an object with several fields/properties. These properties have custom attributes.
What i want is to get the custom attribute from a specific propertie without all the knowlege of the object.
The are the base classes
// FieldAttr has a public Text propery
public class TestObject
{
// Declare fields
[FieldAttr("prop_testfld1")]
public FLDtype1 testfld1 = new FLDtype1();
[FieldAttr("prop_testfld2")]
public FLDtype2 testfld2 = new FLDtype2();
[FieldAttr("prop_testfld3")]
public FLDtype1 testfld3;
}
public class FLDtype1
{
public string Value { get; set; }
}
public class FLDtype2
{
public Guid Value { get; set; }
}
public sealed class FieldAttr: System.Attribute
{
private string _txt;
public EntityFieldType(string txt)
{
this._text = txt;
}
public string Text { get { return this._text; } }
}
And i want to be able to do this in my application:
static void Main(string[] args)
{
TestObject test = new TestObject();
// (Option 1: preferred)
Console.WriteLine(test.testfld1.getFieldAttr().Text);
// (Option 2)
Console.WriteLine(test.getFieldAttr(test.testfld1).Text);
}
Is this possible? I've seen methods to get custom attribute values from all properties/fields of an object, but not for a specific field.
I've got a working method to get custom attribute from an enum, but wasn't able to recreate it for object fields/properties. This is because i couldn't get the name of the field i was trying to explore, because (for example) test.testfld1.ToString() give's me "ns.FLDtype1".
Looking forward for the answer :)
(and excuse my english)
Yes it is possible:
public static class Extensions
{
public static FieldAttr GetFieldAttr(
this TestObject source,
Expression<Func<TestObject,object>> field)
{
var member = field.Body as MemberExpression;
if (member == null) return null; // or throw exception
var fieldName = member.Member.Name;
var test = typeof (TestObject);
var fieldType = test.GetField(fieldName);
if (fieldType != null)
{
var attribute = fieldType.GetCustomAttribute<FieldAttr>();
return attribute;
}
return null;
}
}
Usage:
TestObject test = new TestObject();
var attr = test.GetFieldAttr(x => x.testfld3);
if(attr != null) Console.WriteLine(attr.Text);
Here is the fiddle
After another day of trial and error I decided to make use of Selman22 answer with a little modification. This is code I created:
public class TestObject : iTestObject
{
// Declare fields
[FieldAttr("prop_testfld1")]
public FLDtype1 testfld1 = new FLDtype1();
[FieldAttr("prop_testfld2")]
public FLDtype2 testfld2 = new FLDtype2();
[FieldAttr("prop_testfld3")]
public FLDtype1 testfld3;
}
public class FLDtype1 : iField
{
public string Value { get; set; }
}
public class FLDtype2 : iField
{
public Guid Value { get; set; }
}
public sealed class FieldAttr: System.Attribute
{
private string _txt;
public FieldAttr(string txt)
{
this._txt = txt;
}
public string Text { get { return this._txt; } }
}
public interface iField { }
public interface iTestObject { }
public static class Extensions
{
public static FieldAttr GetFieldAttr<T>(this T source, Expression<Func<iField>> field) where T : iTestObject
{
// Get member body. If no body present, return null
MemberExpression member = (MemberExpression)field.Body;
if (member == null) { return null; }
// Get field info. If no field info present, return null
FieldInfo fieldType = typeof(T).GetField(member.Member.Name);
if (fieldType == null) { return null; }
// Return custom attribute
return fieldType.GetCustomAttribute<FieldAttr>();
}
}
Usage:
public class Program
{
public static void Main()
{
TestObject test = new TestObject();
Console.WriteLine(test.GetFieldAttr(() => test.testfld1).Text);
Console.WriteLine(test.GetFieldAttr(() => test.testfld2).Text);
Console.WriteLine(test.GetFieldAttr(() => test.testfld3).Text);
}
}
Uses:
using System;
using System.Linq;
using System.Reflection;
using System.Linq.Expressions;
I have implemented interfaces to protect the GetFieldAttr method
#Sulman22: Thnx for the response!

Getting data from property attribute

I have a view model which uses custom attributes such as
public int Id { get; set; }
public string Name { get; set; }
[IsEnumeration(typeof(CaseStatus))]
public string Status { get; set; }
IsEnumeration is a custom attribute which takes an Enumeration superclass as a parameter (actually it takes any type, but that doesn't matter since noone else will be using this)
public class IsEnumerationAttribute : Attribute
{
public Type Enumeration;
public IsEnumerationAttribute(Type enumeration)
{
Enumeration = enumeration;
}
}
What I want is to be able to get the type specified for any parameter. Currently my code looks like this:
public T EnumerationValuesToDisplayNames<T>(T item) where T : new()
{
if (LoggedInUser.IsSuper) return item;
var tProps = typeof (T).GetProperties()
.Where(prop => Attribute
.IsDefined(prop, typeof (IsEnumerationAttribute)));
foreach (var prop in tProps)
{
if (prop.GetValue(item, null) != null)
{
/*
Here I look through all properties with the IsEnumerable attribute.
I want to do something such as:
var type = prop.GetAttribute(item, typeof(IsEnumerable));
var displayName = Enumeration<type>.FromId(prop.GetValue(item, null));
prop.SetValue(item, displayName);
*/
}
}
return item;
}
I hope this makes sense, any help would be greatly appreciated, thanks
Assuming from your post you have a class defined as such:
public class Enumeration<T> {
public static string FromId(string id) {
// FromId Implmentation
}
}
Then you should just need
foreach (var prop in tProps) {
var id=prop.GetValue(item, null);
if (id!=null) {
var type = prop.GetCustomAttributes(typeof(EnumerationAttribute>,true).OfType<EnumerationAttribute>().Select(x=>x.Enumeration).First();
var enumerationType=typeof(Enumeration<>).MakeGenericType(type);
var fromIdMethod=enumerationType.GetMethod("FromId",BindingFlags.Public|BindingFlags.Static|BindingFlags.InvokeMethod);
var displayName=fromIdMethod.Invoke(null,new object[] {id});
prop.SetValue(item, displayName);
}
}
Alternatively you could implement the FromId method directly in the EnumerationAttribute then you could just call it directly like so...
foreach (var prop in tProps) {
var id=prop.GetValue(item, null);
if (id!=null) {
var enumAttrib = prop.GetCustomAttributes(typeof(EnumerationAttribute>,true).OfType<EnumerationAttribute>().First();
var displayName=enumAttrib.FromId((string)id);
prop.SetValue(item, displayName);
}

Categories