欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > Unity C#底层原理(二)

Unity C#底层原理(二)

2025/5/3 18:21:12 来源:https://blog.csdn.net/qq_42489774/article/details/140658319  浏览:    关键词:Unity C#底层原理(二)
委托
  1. 方法的容器:委托可以存储一个或多个方法的引用。可以使用委托对象来调用这些方法。
  2. 函数/方法的变量类型:委托类型可以像变量一样声明和使用,存储方法的引用。
  3. 存储、传递方法:委托可以作为参数传递给方法,也可以作为方法的返回值。这使得方法可以接收其他方法作为参数,或返回方法。
  4. 本质是一个类:每个委托类型在编译时都会生成一个类,继承自 System.Delegate 或
    System.MulticastDelegate。这个类提供了 Invoke 方法,用于调用委托所指向的方法。
事件

与委托使用方法一样
不同点:事件不可以在外部被调用,不可以在外部置空
只可以在外部进行+=、-=的操作

 public class Card  {public Card(){Test.MAC?.Invoke();Test.MAC_args?.Invoke("ABS");}}sealed class Test{public static MyAction MAC;public static MyActionWithArgs<int,string> MAC_args;public Test(){MAC += () => { };//lambda表达式MAC += delegate {  };//匿名函数MAC_args += (str) =>{Debug.LogError(str);return 1;};MAC_args += delegate(string str) { Debug.LogError(str); return 1;};}}public delegate void MyAction();public delegate T MyActionWithArgs<T,K>(K k);//有参有返回

协变

1、out 修饰的泛型类型 只能作为返回值,不能作为参数(如Func)
2、父类泛型委托容器可以装子类泛型委托容器(如下方法)

 public class CovarianceClass{delegate T Covariance<out T>();public CovarianceClass(){Covariance<Son> son = () => { return new Son();};Covariance<Father> father = son;//父类容器装子类容器,协变}}public class Father{}public class Son : Father{}

逆变

1、in 修饰的泛型类型,表示只能作为参数,不能返回(如Action)
2、子类泛型容器可以装父类泛型容器(如下方法)

 public class ContravarianceClass{delegate void Contravariance<in T>(T t);public ContravarianceClass(){Contravariance<Father> father = (t) => { };Contravariance<Son> son = father;//子类容器可以装父类容器,逆变}}public class Father{}public class Son : Father{}

typeof

用于获取Type

   public enum SpAtlasEnum{Cards_00,}SpAtlasEnum A = (SpAtlasEnum)Enum.Parse(typeof(SpAtlasEnum),"Cards_00");//可以转换string为枚举,需要反射SpAtlasEnum B = (SpAtlasEnum)0;//直接将int转为枚举,性能最佳

Type

dll一般需要运行过才会生成

        public GameManager(){Assembly assembly = Assembly.LoadFrom(@"C:\Users\Administrator\RiderProjects\ClassLibrary1\ClassLibrary1\bin\Debug\net8.0\ClassLibrary1.dll");//找到这个dllType[] assemblyAllInfo = assembly.GetTypes();for (int i = 0; i < assemblyAllInfo.Length; i++){Debug.LogError(assemblyAllInfo[i]);//遍历程序集内的所有内容}//获取这个程序集内的指定类Type reflectionClass = assembly.GetType("Reflection_Namespace.Reflection_Class");//将他实例化object obj = Activator.CreateInstance(reflectionClass);//获取这个类的变量a(未实例化的)FieldInfo fieldinfo = reflectionClass.GetField("a");//设置该变量a指定 实例化的obj  fieldinfo.SetValue(obj,131);//打印实例化的obj的变量a的值Debug.LogError(fieldinfo.GetValue(obj));//同理 获取方法(未实例化的)MethodInfo methodInfo = reflectionClass.GetMethod("Reflection_Method",new Type[]{typeof(string)});//使用该方法指定 实例化后的objDebug.LogError(methodInfo.Invoke(obj, new object[] {"Reflection" })); }}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词