[英]C# Reflection get the instance behind FieldInfo
I am having an issue with Reflection, which I can't seem to find a solution for.我遇到了反射问题,我似乎找不到解决方案。
I have the following simple interface:我有以下简单的界面:
public interface IDataProperty<T>
{
public T Value { get; set; }
public int BytesCount();
public byte[] Serialize();
}
the struct which implements the interface above:实现上述接口的结构:
public struct IntProperty : IDataPropery<int>
{
private int _value;
public int Value { get => _value; set => _value = value; }
public int BytesCount()
{
return 4;
}
public byte[] Serialize()
{
return BitConverter.GetBytes(_value);
}
public IntDataProperty(int value) { _value = value; }
}
and a simple class to hold the values:和一个简单的 class 来保存值:
public class ValuesContainer
{
public IntProperty prop1;
public IntProperty prop2;
}
I am trying to call the Serialize()
method on both prop1
and prop2
in my Processor class, with no luck so far... :我正在尝试在我的处理器 class 的prop1
和prop2
上调用Serialize()
方法,但到目前为止没有运气......:
public class Processor
{
public void ProccesData<T>(out T result) where T : ValuesContainer, new()
{
result = new T();
List<FieldInfo> dataFields = new List<FieldInfo>();
result.GetType().GetFields().ToList().ForEach(field => {
if(field.FieldType.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDataProperty<>)))
{
dataFields.Add(field);
}
});
MethodInfo serializeMI = typeof(IDataProperty<>).GetMethod("Serialize");
foreach(FieldInfo field in dataFields)
{
Console.WriteLine(field.Name);
serializeMI.Invoke(field,null);
}
}
}
Running the code at this point gives me the following error:此时运行代码会出现以下错误:
'Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true.'
I am aware that I need to get somehow to the instance behind the field
variable, but have no idea how to do it.我知道我需要以某种方式获取field
变量后面的实例,但不知道该怎么做。 Does anyone know a good way of doing what i am trying to achieve using other methods, or only Reflection is the way to go, and if the latter - what solutions do I have?有谁知道用其他方法做我正在尝试实现的事情的好方法,或者只有反射是通往 go 的方式,如果是后者 - 我有什么解决方案?
Thanks in advance to all of you.在此先感谢大家。
After the great help @JeroenMostert provided in the comments, i have been able to solve the issue at its roots.在评论中提供@JeroenMostert 的大力帮助后,我已经能够从根本上解决问题。 As long as Jeroen haven't provided an answer to be accepted as correct, I will just show his solution, so this question gets marked as answered.只要 Jeroen 没有提供被接受为正确的答案,我就会展示他的解决方案,所以这个问题被标记为已回答。 Still, the benefit here goes to @JeroenMostert不过,这里的好处是@JeroenMostert
internal class Program
{
static void Main(string[] args)
{
ValuesContainer container = new ValuesContainer();
Processor processor = new Processor();
processor.ProccesData(out container);
}
}
public interface IDataProperty<T>
{
public void Serialize();
}
public struct IntProperty : IDataProperty<int>
{
private int _value;
public int Value { get => _value; set => _value = value; }
public int BytesCount()
{
return 4;
}
public void Serialize()
{
Console.WriteLine("I have been invoked !" + this.GetHashCode());
}
}
public class ValuesContainer
{
public IntProperty prop1;
public IntProperty prop2;
}
public class Processor
{
public void ProccesData<T>(out T result) where T : ValuesContainer, new()
{
result = new T();
List<FieldInfo> dataFields = new List<FieldInfo>();
result.GetType().GetFields().ToList().ForEach(field => {
if (field.FieldType.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDataProperty<>)))
{
dataFields.Add(field);
}
});
foreach (FieldInfo field in dataFields)
{
Console.WriteLine("Invoking 'Serialize' method on fieldName :" + field.Name);
field.GetValue(result).GetType().GetMethod("Serialize").Invoke(field.GetValue(result), null);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.