夜光带你走进C# 游戏开发等(九十九)擅长的领域

夜光序言:

 

岳飞《满江红》怒发冲冠,凭栏处、潇潇雨歇。抬望眼,仰天长啸,壮怀激烈。三十功名尘与土,八千里路云和月。莫等闲,白了少年头,空悲切。靖康耻,犹未雪;臣子恨,何时灭!驾长车,踏破贺兰山缺。壮志饥餐胡虏肉,笑谈渴饮匈奴血。待从头,收拾旧山河,朝天阙!

 

 

 

 

 

 

 

 

 

 

 

 

正文:

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace 案例1
{
    //反射:在程序运行时,可以知道一个类有哪些成员,每个成员上有哪些标志、几个参数、每个参数的类型、返回值类型

    //1.定义一个从Attribute继承的基类
    class CommentAttribute : Attribute
    {
        public CommentAttribute(string s)
        {

        }

    }

    //2.可以给类或方法等加上一个标志

    [Comment("游戏对象的基类")]
    class GameObject
    {
        private int i;

        public float G { get; set; }

        [Comment("这是一个方法")]
        public void f()
        {

        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            //取得一个类的信息:typeof(类名)
            //返回:类的成员信息类  有点绕~
            TypeInfo mi = typeof(GameObject).GetTypeInfo();
            
            //取得类名
            Console.WriteLine(mi.Name);

            //取得类中的成员
            MemberInfo[] miArray = mi.GetMembers();

            //写一个for循环
            for(int i = 0; i < miArray.Length; i++)
            {
                MemberInfo ni = miArray[i]; //取得成员数组中第i个成员
                Console.WriteLine(ni.Name);  //取得该成员的名称
            }

            Console.ReadLine();

        }
    }
}

 

 

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace 案例3
{
    //这里我们主要讲下反射
    class UpdateAttribute : Attribute
    {

    }

    class A
    {
        [Update]
        public void aa(int i)
        {
            Console.WriteLine("aa{0}", i);
        }
    }


    class B
    {
        [Update]
        public void bb(int i)
        {
            Console.WriteLine("bb{0}", i);
        }
    }

    class Program
    {

        /// <summary>
        /// 遍历一个类中所有的方法,找到并返回被标记为update的方法
        /// </summary>
        /// <param name="ti">一个类的类型信息</param>
        /// <returns>如果找到返回该方法的方法信息,否则返回空</returns>
        static MethodInfo GetUpdateMethod(TypeInfo ti)
        {
            //取到所有的方法
            MethodInfo[] miArray = ti.GetMethods();

            //下面我们再写一个循环
            for(int i = 0; i < miArray.Length; i++)
            {
                MethodInfo mi = miArray[i];
                //接下来就是判断的过程
                //判断mi是否为一个被标记为update的方法
                if (mi.GetCustomAttribute<UpdateAttribute>() != null) //用的是一种叫泛型的方法,之前讲过
                {
                    return mi;
                }
            }
            return null; //表示没有找到
        }

        static void Main(string[] args)
        {
            //1. 遍历类A和类B,看看有没有标记为update的方法,有的话,取出该方法

            MethodInfo miA = GetUpdateMethod(typeof(A).GetTypeInfo()); //aa
            MethodInfo miB = GetUpdateMethod(typeof(B).GetTypeInfo());  //bb

            A a = new A();
            B b = new B();

            //2. 在游戏循环中每帧调用该方法
            for (int i = 0; !Console.KeyAvailable; i++)
            {
                miA.Invoke(a, new object[] { i });
                miB.Invoke(b, new object[] { i });
            }
            Console.ReadLine();
        }
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值