【发布时间】:2019-11-28 22:01:44
【问题描述】:
我有一个带有接口 IDialogueAnimation 的公共类 Typewriter。在 DialoguePrinter 类的一个方法中,我使用接口 IDialogueAnimation 获取所有对象。它们以类型的形式出现,我想将它们转换为 IDialogueAnimation。但是,它不会让我得到“InvalidCastException:指定的演员表无效”。错误。为什么是这样?谢谢!
我检查了 Typewriter 和 IDialogueAnimation 是否在同一个程序集中(这是我尝试寻找解决方案时出现的问题)。
IDialogueAnimation GetAnimationInterfaceFormName(string name)
{
Type parentType = typeof(IDialogueAnimation);
Assembly assembly = Assembly.GetExecutingAssembly();
Type[] types = assembly.GetTypes();
IEnumerable<Type> imp = types.Where(t => t.GetInterfaces().Contains(parentType));
foreach (var item in imp)
{
if (item.Name.ToLower() == name.ToLower())
{
return (IDialogueAnimation) item;
}
}
Debug.LogError("Can't find any animation with name " + name);
return null;
}
这是界面
public interface IDialogueAnimation
{
bool IsPlaying { get; set; }
IEnumerator Run(OrderedDictionary wordGroup, float speed);
}
【问题讨论】: