private object GetVariableArray(Array value, string varNameInShader)
{
Type t = value.GetType().GetElementType();
Type varType;
if (variableArrayDict == null)
{
variableArrayDict = new Dictionary<Type, Type>();
Type baseType = typeof(CSharpGL.UniformArrayVariableBase);
Assembly asm = Assembly.GetAssembly(baseType);
var types = from item in asm.GetTypes()
where (baseType.IsAssignableFrom(item)
&& (!item.IsAbstract)
&& (!item.IsGenericType))
select item;
foreach (Type item in types)
{
// example: variableDict.Add(typeof(int), typeof(UniformInt32));
bool found = false;
foreach (PropertyInfo propertyInfo in item.GetProperties())
{
if (propertyInfo.GetCustomAttributes(typeof(UniformValueAttribute), true).Count() > 0)
{
// example: variableArrayDict.Add(typeof(float), typeof(UniformFloatArray));
variableArrayDict.Add(item.BaseType.GetGenericArguments()[0], item);
found = true;
break;
}
}
if (!found)
{
throw new Exception(string.Format("No property in [{0}] is marked with [{1}].", item, typeof(UniformValueAttribute)));
}
}
}
if (variableArrayDict.TryGetValue(t, out varType))
{
return Activator.CreateInstance(varType, varNameInShader, value.Length);
}
else
{
throw new Exception(string.Format(
"UniformVariable type [{0}] doesn't exists or not included in the variableDict!",
t));
}
}