Attribute特性应用小案例-俄罗斯世界杯球迷入境

前言:
最近写了几篇Attribute的文章,也是对快要遗忘的知识进行一次温习,想着用特性实现一个小小的例子,就以这次俄罗世界杯
球迷入境的为例。

简要说明:为了方便球迷出入俄罗斯观看世界杯比赛,在球迷购买了正式比赛任意场次的门票以后,可以通过球票在FIFA的官网申请FAN ID。

这是一个球迷的证件 ,上面有你护照的信息,姓名等等,FAN ID有如下几个作用:
1.凭借FAN ID 球迷可以在世界杯期间无限次的进出俄罗斯,这张FAN ID就相当于俄罗斯的签证Visa。
2.比赛日当天,凭FAN ID可以免费乘坐公共交通,如地铁,公交,甚至是往返城市的铁路,均是免费的。
3.进入球场,除了当前场次比赛门票,也需要验证FAN ID,两者均满足,才能够进入体育场内。

下面就简单以这个小例子来演示一下特性Attribute的应用。实现两个功能:

1.公民入境,持有俄罗斯的VISA或者球迷FAN ID均可入境,有FAN ID就不需要单独申请VISA了。
2.比赛日当天,凭FAN ID可以免费乘坐公共交通,只有FAN ID可以。

如下:

[AttributeUsage(AttributeTargets.Field)]
public sealed class IDENTITY_AUTHAttribute:System.Attribute
{
    public string FAN_ID{ get; set;}
    public string FAN_NAME{ get; set;}
    public string VISA_ID{ get; set;}

}

说明:
定义IDENTITY_AUTHAttribute特性类,定义三个属性properties,FAN_ID,FAN_NAME,VISA_ID,
通常有FAN_ID就可以了,ID代表着数据的标识,但我们在入境的途中,发生了一件小插曲,我的名字叫WANG HUAN,我们在FIFA注册的时候,用的英文名字的习惯,姓和名是反着的,HUAN WANG,但入境时,俄罗斯那边说你的名字和你护照的的名字不符合,要求你改掉(真的很死板),所以名字反了,也不行,必须打电话进行更正.....

VISA_ID 即是你申请了俄罗斯的签证,并假定他在有效期内。

将该特性应用在下面的公民上面,标识身份。

public class Citizen{
    public string passportNumber;
    public string name;
    public int age;
    public int gender;//1-male 2 female

    public Citizen(string passportNumber,string name,int age,int gender)
    {
        this.passportNumber = passportNumber;
        this.name = name;
        this.age = age;
        this.gender = gender;

    }

}

说明:

定义Citizen类,添加了一些常用的字段Field,主要用到pasportNumber和name,假定都已经申请了护照。名字就是上面提到的,即便你有了FAN ID,名字也是要进行验证的。

public class AirportCheckIn : MonoBehaviour {

    [IDENTITY_AUTH(FAN_ID="132993994",FAN_NAME="huanwang")]
    public Citizen wanghuan;

    [IDENTITY_AUTH(FAN_ID="663938249",FAN_NAME="feipeng")]
    public Citizen pengfei;

    [IDENTITY_AUTH(FAN_ID="991240982",FAN_NAME="gumenghua")]
    public Citizen gumenghua;

    [IDENTITY_AUTH(VISA_ID="9865326014343")]
    public Citizen bajia;

    [IDENTITY_AUTH]
    public Citizen hulei;



    public List CitizenList = new List ();
    // Use this for initialization
    void Start () {
        wanghuan = new Citizen ("10001", "wanghuan", 29, 1);
        pengfei = new Citizen ("10002", "pengfei", 30, 1);
        gumenghua = new Citizen ("10003", "gumenghua", 32, 2);
        bajia = new Citizen("10004","bajia",29,2);
        hulei = new Citizen ("10005", "hulei", 33, 1);

        CitizenList.Add (wanghuan);
        CitizenList.Add (pengfei);
        CitizenList.Add (gumenghua);
        CitizenList.Add (bajia);
        CitizenList.Add (hulei);

        //check in
        foreach (var citizen in CitizenList) {
            CheckIn (citizen);
        }

        //free public transportation
        foreach (var citizen in CitizenList) {
            CheckPublicTransportationFree (citizen);
        }


    }

    /// 
    /// Checks in
    /// 
    /// true, if in was checked, false otherwise.
    /// Value.
    public bool CheckIn(Citizen val)
    {
        
        Type t = typeof(AirportCheckIn);//.GetType ();
        FieldInfo info = t.GetField (val.name);

        if (info.IsDefined (typeof(IDENTITY_AUTHAttribute),false)) {
         
            IDENTITY_AUTHAttribute attribute = (IDENTITY_AUTHAttribute)Attribute.GetCustomAttribute (info, typeof(IDENTITY_AUTHAttribute));
            if (attribute != null) {
                string str = "";
                if (!string.IsNullOrEmpty(attribute.VISA_ID)) {
                    //通过VISA入境
                    Debug.Log(val.name+" has the VISA of Russia.But do not have permission for free Public Transportation.");
                } else {
                    str = val.name+" do NOT have the VISA of Russia.";

                    if (!string.IsNullOrEmpty (attribute.FAN_ID)) {
                        str += " But " + val.name + " has the FAN ID,we need to check it!";
                        if (attribute.FAN_NAME.Equals (val.name)) {//compare with name
                            str += val.name + " FAN ID was approved,Public Transportation are free for you.";
                            Debug.Log (str);
                        } else {
                            str += " Unfortunately," + val.name + " FAN ID is not same as passport name,please call FIFA FAN ID CENTER to change it!";
                            Debug.Log (str);
                            return false;
                        }

                    } else {
                        Debug.Log(val.name+" do not have VISA and FAN ID! REJECTED!");
                        return false;
                    }
                }
            }
        }
        return true;
    }

    /// 
    /// Checks the public transportation free.
    /// 
    /// true, if public transportation free was checked, false otherwise.
    /// Value.
    public bool CheckPublicTransportationFree(Citizen val)
    {
        Type t = typeof(AirportCheckIn);//.GetType ();
        FieldInfo info = t.GetField (val.name);

        if (info.IsDefined (typeof(IDENTITY_AUTHAttribute),false)) {

            IDENTITY_AUTHAttribute attribute = (IDENTITY_AUTHAttribute)Attribute.GetCustomAttribute (info, typeof(IDENTITY_AUTHAttribute));
            if (attribute != null) {
                string str = "";
                if (string.IsNullOrEmpty(attribute.FAN_ID)) {
                    Debug.Log(val.name+" do not have FAN ID,you should buy tickets.");
                } else {
                    str += val.name + " has the FAN ID.";
                    if (attribute.FAN_NAME.Equals (val.name)) {//compare with name
                        str += "and FAN ID was approved,Public Transportation are free for you! Enjoy the FIFA World Cup Show!";
                        Debug.Log (str);
                    } else {
                        str += " Unfortunately," + val.name + " FAN ID is not same as passport name,please call FIFA FAN ID CENTER to change it!";
                        Debug.Log (str);
                        return false;
                    }
                    }
                }
            }
        return true;
        
    }


}

说明:
有点长,但内容非常简单,首先定义了几个公民:

[IDENTITY_AUTH(FAN_ID="132993994",FAN_NAME="huanwang")]
    public Citizen wanghuan;

    [IDENTITY_AUTH(FAN_ID="663938249",FAN_NAME="feipeng")]
    public Citizen pengfei;

    [IDENTITY_AUTH(FAN_ID="991240982",FAN_NAME="gumenghua")]
    public Citizen gumenghua;

    [IDENTITY_AUTH(VISA_ID="9865326014343")]
    public Citizen bajia;

    [IDENTITY_AUTH]
    public Citizen hulei;

每个公民指定了不同的身份认证信息,如第一个wanghuan,指定了FAN_ID以及FAN_NAME(注意FAN_NAME是反的)
第二个,第三个都指定了FAN_ID&&FAN_NAME,意味着三个人均要通过FAN_ID的形式入境。
第四个是申请了俄罗斯签证,基本上是去旅行的(看球是必须要有FAN ID才能进场)
最后一个是没有身份信息,没有FAN_ID也没有VISA_ID,只是有中国公民的护照。

wanghuan = new Citizen ("10001", "wanghuan", 29, 1);
        pengfei = new Citizen ("10002", "pengfei", 30, 1);
        gumenghua = new Citizen ("10003", "gumenghua", 32, 2);
        bajia = new Citizen("10004","bajia",29,2);
        hulei = new Citizen ("10005", "hulei", 33, 1);

        CitizenList.Add (wanghuan);
        CitizenList.Add (pengfei);
        CitizenList.Add (gumenghua);
        CitizenList.Add (bajia);
        CitizenList.Add (hulei);

进行基本的初始化,并添加到容器中。

//check in
        foreach (var citizen in CitizenList) {
            CheckIn (citizen);
        }

        //free public transportation
        foreach (var citizen in CitizenList) {
            CheckPublicTransportationFree (citizen);
        }

说明:

测试两个功能:
1.入境身份核验,通过反射获取IDENTITY_AUTH特性,判断VISA_ID,如果没有则判断FAN_ID&&FAN_NAME
2.是否有权限乘坐免费的公共交通(假定当天是比赛日,并且有球票),通过反射获取IDENTITY_AUTH特性,判断FAN_ID&&FAN_NAME

功能一:

/// 
    /// Checks in
    /// 
    /// true, if in was checked, false otherwise.
    /// Value.
    public bool CheckIn(Citizen val)
    {
        
        Type t = typeof(AirportCheckIn);//.GetType ();
        FieldInfo info = t.GetField (val.name);

        if (info.IsDefined (typeof(IDENTITY_AUTHAttribute),false)) {
         
            IDENTITY_AUTHAttribute attribute = (IDENTITY_AUTHAttribute)Attribute.GetCustomAttribute (info, typeof(IDENTITY_AUTHAttribute));
            if (attribute != null) {
                string str = "";
                if (!string.IsNullOrEmpty(attribute.VISA_ID)) {
                    //通过VISA入境
                    Debug.Log(val.name+" has the VISA of Russia.But do not have permission for free Public Transportation.");
                } else {
                    str = val.name+" do NOT have the VISA of Russia.";

                    if (!string.IsNullOrEmpty (attribute.FAN_ID)) {
                        str += " But " + val.name + " has the FAN ID,we need to check it!";
                        if (attribute.FAN_NAME.Equals (val.name)) {//compare with name
                            str += val.name + " FAN ID was approved,Public Transportation are free for you.";
                            Debug.Log (str);
                        } else {
                            str += " Unfortunately," + val.name + " FAN ID is not same as passport name,please call FIFA FAN ID CENTER to change it!";
                            Debug.Log (str);
                            return false;
                        }

                    } else {
                        Debug.Log(val.name+" do not have VISA and FAN ID! REJECTED!");
                        return false;
                    }
                }
            }
        }
        return true;
    }

功能二:

/// 
    /// Checks the public transportation free.
    /// 
    /// true, if public transportation free was checked, false otherwise.
    /// Value.
    public bool CheckPublicTransportationFree(Citizen val)
    {
        Type t = typeof(AirportCheckIn);//.GetType ();
        FieldInfo info = t.GetField (val.name);

        if (info.IsDefined (typeof(IDENTITY_AUTHAttribute),false)) {

            IDENTITY_AUTHAttribute attribute = (IDENTITY_AUTHAttribute)Attribute.GetCustomAttribute (info, typeof(IDENTITY_AUTHAttribute));
            if (attribute != null) {
                string str = "";
                if (string.IsNullOrEmpty(attribute.FAN_ID)) {
                    Debug.Log(val.name+" do not have FAN ID,you should buy tickets.");
                } else {
                    str += val.name + " has the FAN ID.";
                    if (attribute.FAN_NAME.Equals (val.name)) {//compare with name
                        str += "and FAN ID was approved,Public Transportation are free for you! Enjoy the FIFA World Cup Show!";
                        Debug.Log (str);
                    } else {
                        str += " Unfortunately," + val.name + " FAN ID is not same as passport name,please call FIFA FAN ID CENTER to change it!";
                        Debug.Log (str);
                        return false;
                    }
                    }
                }
            }
        return true;
        
    }

控制台输出:

aaa222.png

到此为止,如果大家发现有什么不对的地方,欢迎指正,共同提高,感谢您的阅读!

编辑于2018.7.17

--闲言碎语

世界杯已经圆满结束了,这一个月感觉很漫长,太多精彩难忘的瞬间,刚才听到球迷说,持有FANID可以免签入境到2018年底,另人振奋的消息,朋友说,好想坐火车去一次俄罗斯,欣赏贝加尔湖,我也很心动,未来一定有机会,包括两年后的欧洲杯,眼下要尽早的恢复工作,痛快快的干一场!

C.png

(总裁,精神领袖!!!)

你可能感兴趣的:(Attribute特性应用小案例-俄罗斯世界杯球迷入境)