unity3D技术分享

c#反射实例化MissingMethodException: C

2022-04-13  本文已影响0人  好怕怕

MissingMethodException: Constructor on type 'TestClass' not found.
System.RuntimeType.CreateInstanceImpl (System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes, System.Threading.StackCrawlMark& stackMark) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Activator.CreateInstance (System.Type type, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Activator.CreateInstance (System.Type type, System.Object[] args) (at <eae584ce26bc40229c1b1aa476bfa589>:0)

问题是构造函数不能私有,对于抽象类abstract,也不可以反射

 public class TestClass
    {
        private int aa;
        protected TestClass(object[] args)
        {
            aa = 0;
        }

        protected Vector3 m_Position = Vector3.zero;
        public virtual Vector3 Position
        {
            get
            {
                return m_Position;
            }
            set
            {
                m_Position = value;
            }
        }

    }

var aa = SceneUtils.CreateInstance<TestClass>(new object[1]);

上一篇下一篇

猜你喜欢

热点阅读