Reduce the perception of switch branches in Forum discussions and use features to reflect the classes to be used. Forum discussions on switch

Source: Internet
Author: User

Reduce the perception of switch branches in Forum discussions and use features to reflect the classes to be used. Forum discussions on switch

On the Forum today, I asked how to reduce the switch branch.

I thought for myself and thought that using features can directly reduce the switch judgment, so I wrote some

It indicates that the efficiency may not be comparable to that of the switch.

Let's get started.

First set the interface

public interface IGetExec    {        string GetResult();    }

Then implement

public class GetExec : IGetExec    {        public string GetResult()        {            return "get";        }    }    public class Get1Exec : IGetExec    {        public string GetResult()        {            return "get1";        }    }    public class GetTestExec : IGetExec    {        public string GetResult()        {            return "gettest";        }    }
Create features

[AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = true)]    sealed class TestAttribute : Attribute    {        private IGetExec ExecObj { get; set; }        public TestAttribute(Type t)        {            this.ExecObj = Activator.CreateInstance(t) as IGetExec;        }        public string Exec()        {            return ExecObj.GetResult();        }    }
Define an Enum to determine which class to use

public enum TestEnum    {        [Test(typeof(GetExec))]        get,        [Test(typeof(Get1Exec))]        getTest,        [Test(typeof(GetTestExec))]        get1    }

Then extend the Enum method.

public static class EnumClass    {        public static string Exec(this TestEnum e)        {            Type t = e.GetType();            var file = t.GetField(e.ToString());            var att = file.GetCustomAttributes(typeof(TestAttribute), false).Single();            if (att != null)            {                return (att as TestAttribute).Exec();            }            else                return null;        }    }

Last call

public class Program    {        static void Main(string[] args)        {            Console.WriteLine(TestEnum.get.Exec());            Console.Read();        }    }

You only need to extend the IGetExec interface and then add enum to implement the extension function.

How to solve the switch branch statement through reflection in java

Do you want to determine which type of instance is generated in the factory mode?
Class clazz = Class. forName (Class Name/* string */);
Clazz. newInstance () to generate an instance of the class requires type conversion
In this way, no switch is required.
How to Use AS3 reflection (how to replace switch with getDefinitionByName ?)

The getDefinitionByName () function public function getDefinitionByName (name: String): Object language version: ActionScript 3.0 RuntimeVersions: AIR 1.0, Flash Player 9 returns the Class Object Reference of the class specified by the name parameter. Parameter name: String-class name. Return Object-return Class Object Reference of the class specified by the name parameter.
Raise ReferenceError-there is no public definition with the specified name.

The following example uses the GetDefinitionByNameExample class to create an Orange square on the stage. This is done using the following steps: declare variables that represent the orange background color and 80 pixel size, which will be used to draw a square later. In the constructor, assign the Class type variable ClassReference to Sprite. Instantiate the ClassReference instance. Since the instance is a Sprite object by reference, you can use the method that can be used for Sprite to draw a square and add it to the display list. Package {import flash. display. displayObject; import flash. display. sprite; import flash. utils. getDefinitionByName; public class GetDefinitionByNameExample extends Sprite {private var bgColor: uint = 0xFFCC00; private var size: uint = 80; public function callback () {var ClassReference: Class = getDefinitionByName ("flash. display. sprite ") as Class; var instance: Object = new ClassReference (); instance. graphics. beginFill (bgColor); instance. graphics. drawRect (0, 0, size, size); instance. graphics. endFill (); addChild (DisplayObject (instance ));}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.