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<Citizen> CitizenList = new List<Citizen> ();
    // 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);
        }


    }

    /// <summary>
    /// Checks in
    /// </summary>
    /// <returns><c>true</c>, if in was checked, <c>false</c> otherwise.</returns>
    /// <param name="val">Value.</param>
    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;
    }

    /// <summary>
    /// Checks the public transportation free.
    /// </summary>
    /// <returns><c>true</c>, if public transportation free was checked, <c>false</c> otherwise.</returns>
    /// <param name="val">Value.</param>
    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

功能一:

/// <summary>
    /// Checks in
    /// </summary>
    /// <returns><c>true</c>, if in was checked, <c>false</c> otherwise.</returns>
    /// <param name="val">Value.</param>
    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;
    }

功能二:

/// <summary>
    /// Checks the public transportation free.
    /// </summary>
    /// <returns><c>true</c>, if public transportation free was checked, <c>false</c> otherwise.</returns>
    /// <param name="val">Value.</param>
    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

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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,539评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,911评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,337评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,723评论 1 290
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,795评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,762评论 1 294
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,742评论 3 416
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,508评论 0 271
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,954评论 1 308
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,247评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,404评论 1 345
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,104评论 5 340
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,736评论 3 324
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,352评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,557评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,371评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,292评论 2 352

推荐阅读更多精彩内容