Here is a method I have in my Utilities toolbelt. I use it from MonoBehaviours in external dll files to access properties and fields of NGUI widgets.
public static void GenericValueSet(object targetObject, string propName, object value)
{
Type objectType = targetObject.GetType();
PropertyInfo info = objectType.GetProperty(propName);
if (info != null && info.CanWrite)
{
Debug.Log("Setting property " + propName + " to " + value.ToString());
info.SetValue(targetObject, value, null);
}
FieldInfo finfo = objectType.GetField(propName);
if (finfo != null && finfo.IsPublic)
{
Debug.Log("Setting public field " + propName + " to " + value.ToString());
finfo.SetValue(targetObject, value);
}
}
Feel free to remove the debug messages and shorten variable names, they are there for troubleshooting purposes when first getting used to using the method. I tried to make it as easy to read as possible.