- Home /
reflection propertyinfo.getvalue compiles fine but gives erros in editor
Soo this is a very weird issue that I have no explanation for. So here is the code :
private void Update(){
GetAnimationCon();
}
private void GetAnimationCon(){
var AnimPreviewInspector=Resources.FindObjectsOfTypeAll(typeof(Editor).Assembly.GetType("UnityEditor.AnimationClipEditor"));
System.Reflection.FieldInfo[] fields = AnimPreviewInspector[0].GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var AnimPreview = new object();
var AnimCon= new object();
foreach (System.Reflection.FieldInfo f in fields){
if(f.name == "m_AvatarPreview")
{
AnimPreview = f.GetValue(AnimPreviewInspector[0];
System.Reflection.PropertyInfo[] props = AnimPreview.GetType().GetProperties(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
foreach(System.Reflection.PropertyInfo p in props){
if(p.GetIndexParameters().Length==0&&p.Name == "Animator")
{
AnimCon = p.GetValue(AnimPreview);
}
}
}
The above code compiles fine in Visual Studio, no complaints. It is done exactly like the examples shown in the C# docs. Now when I look at Unity it tries to tell me that there is no overload for method GetValue that takes 1 arguments. The line numbers are corresponding to wherever I am trying to get the value of a property. I'm like what the hell because it doesn't compile in Visual Studio without an argument. Please help. Maybe its a bug and I can't do anything about it? Below is a link to the documentation for the get value function. Also it doesn't complain about me trying to get fields and that works just fine.
https://msdn.microsoft.com/en-us/library/hh194385(v=vs.110).aspx
Answer by Bunny83 · Apr 26, 2017 at 06:10 PM
Well, read the documentation more carefully ^^
.NET Framework
Available since 4.5
Your target platform in visual studio is set to a higher level than what Unity actually supports. Unity basically supports up to .NET 3.5
So you have to pass the index array as well. However as far as i remember, if the property doesn't have any index parameters you should be able to pass null
.
AnimCon = p.GetValue(AnimPreview, null);
I still use the community version of visual studio 2015 and for me it recognises your code as error.
edit
I just checked the docs, just to be sure:
This value should be null for non-indexed properties.
So yes, passing null
is correct.