簡體   English   中英

從自己內部以靜態方法返回類的新實例?

[英]Return new instance of a class from within itself in a static method?

好的,我現在可以像我一樣執行此操作,但是我想知道它是否可以作為“自動”屬性更無縫地完成。

class Test : PPCNode
    {

        public static PPCNode ParseCode(string code)
        {
            string[] codeArray = SplitCode(code);

            // return instance of the Instruction Node
            return new Test(codeArray, 0);
        }
    }

所以基本上這就是每個人說的代碼。

現在,此代碼發生在許多類中,因此它是相同的,除了實例是它所在類的實例之外。

所以這就是我想知道的,是否有可能通過某種自動方法來創建新實例,例如。

如果您了解我的意思,請“返回新的ThisClass(....)”。

謝謝:)

編輯:

好了,這里有一些例子,這將是很多代碼,但是很容易看透。

所以這是兩個類:

internal class AddicDotInstructionNode : PPCNode
    {
        private readonly string[] parameterArray;

        private AddicDotInstructionNode(string[] codeArray)
        {
            parameterArray = codeArray;

            // Throw exception if the number of parameters is invalid to instruction
            if (codeArray.Length != 3)
                throw new Exception($"\"{GetType().Name}\" instruction is invalid");
        }

        public static PPCNode ParseCode(string code)
        {

            // Split the code string into an array so each element is a parameter
            string[] codeArray = code.Split(',');

            // return instance of the Instruction Node
            return new AddicDotInstructionNode(codeArray);
        }

        // +-------------------------------------+------------+----------------+-------------------------------------------------+
        // | 0                                 5 | 6 7 8 9 10 | 11 12 13 14 15 | 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
        // +-------------------------------------+------------+----------------+-------------------------------------------------+
        // |                  13                 |      D     |        A       |                       SIMM                      |
        // +-------------------------------------+------------+----------------+-------------------------------------------------+
        public override int Compile()
        {
            int opcode = 13;

            int regD = Utility.PPCUtility.GetParameterDigit(parameterArray[0]);
            int regA = Utility.PPCUtility.GetParameterDigit(parameterArray[1]);
            int simm = Utility.PPCUtility.Get16bitHexorDecimalAsUint(parameterArray[2]);

            // opcode at 0-5 (26-31)
            // Register D at 6-10 (21-25)
            // Register A at 11-15 (16-20)
            // SIMM at 16-31 (0-15)
            return opcode << 26 | regD << 21 | regA << 16 | simm;
        }
}

內部類AddicInstructionNode:PPCNode {私有只讀字符串[] parameterArray;

    private AddicInstructionNode(string[] codeArray)
    {
        parameterArray = codeArray;

        // Throw exception if the number of parameters is invalid to instruction
        if (codeArray.Length != 3)
            throw new Exception($"\"{GetType().Name}\" instruction is invalid");
    }

    public static PPCNode ParseCode(string code)
    {

        // Split the code string into an array so each element is a parameter
        string[] codeArray = code.Split(',');

        // return instance of the Instruction Node
        return new AddicInstructionNode(codeArray);
    }

    // +-------------------------------------+------------+----------------+-------------------------------------------------+
    // | 0                                 5 | 6 7 8 9 10 | 11 12 13 14 15 | 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
    // +-------------------------------------+------------+----------------+-------------------------------------------------+
    // |                  12                 |      D     |        A       |                       SIMM                      |
    // +-------------------------------------+------------+----------------+-------------------------------------------------+
    public override int Compile()
    {
        int opcode = 12;

        int regD = Utility.PPCUtility.GetParameterDigit(parameterArray[0]);
        int regA = Utility.PPCUtility.GetParameterDigit(parameterArray[1]);
        int simm = Utility.PPCUtility.Get16bitHexorDecimalAsUint(parameterArray[2]);

        // opcode at 0-5 (26-31)
        // Register D at 6-10 (21-25)
        // Register A at 11-15 (16-20)
        // SIMM at 16-31 (0-15)
        return opcode << 26 | regD << 21 | regA << 16 | simm;
    }
}

這是這些類的調用方:

internal static class IntegerArithmeticInstructionNode
    {
        // PowerPC: Table A-2 Integer Arithmetic Instructions (Complete)
        public static Func<string, PPCNode> ParseInstruction(string code)
        {
            switch (code)
            {
                // Addx
                case "add": return AddInstructionNode.ParseCode;
                case "add.": return AddInstructionNode.ParseCodeRc;
                case "addo": return AddInstructionNode.ParseCodeOe;
                case "addo.": return AddInstructionNode.ParseCodeOeRc;

                // Addcx
                case "addc": return AddcInstructionNode.ParseCode;
                case "addc.": return AddcInstructionNode.ParseCodeRc;
                case "addco": return AddcInstructionNode.ParseCodeOe;
                case "addco.": return AddcInstructionNode.ParseCodeOeRc;

                // Addex
                case "adde": return AddeInstructionNode.ParseCode;
                case "adde.": return AddeInstructionNode.ParseCodeRc;
                case "addeo": return AddeInstructionNode.ParseCodeOe;
                case "addeo.": return AddeInstructionNode.ParseCodeOeRc;

                // Addi
                case "addi": return AddiInstructionNode.ParseCode;

                // Addic
                case "addic": return AddicInstructionNode.ParseCode;

                // Addic.
                case "addic.": return AddicDotInstructionNode.ParseCode;

                // Addis
                case "addis": return AddisInstructionNode.ParseCode;

                // Addmex
                case "addme": return AddmeInstructionNode.ParseCode;
                case "addme.": return AddmeInstructionNode.ParseCodeRc;
                case "addmeo": return AddmeInstructionNode.ParseCodeOe;
                case "addmeo.": return AddmeInstructionNode.ParseCodeOeRc;

                // Addzex
                case "addze": return AddzeInstructionNode.ParseCode;
                case "addze.": return AddzeInstructionNode.ParseCodeRc;
                case "addzeo": return AddzeInstructionNode.ParseCodeOe;
                case "addzeo.": return AddzeInstructionNode.ParseCodeOeRc;

                // Divwx
                case "divw": return DivwInstructionNode.ParseCode;
                case "divw.": return DivwInstructionNode.ParseCodeRc;
                case "divwo": return DivwInstructionNode.ParseCodeOe;
                case "divwo.": return DivwInstructionNode.ParseCodeOeRc;

                // Divwux
                case "divwu": return DivwuInstructionNode.ParseCode;
                case "divwu.": return DivwuInstructionNode.ParseCodeRc;
                case "divwuo": return DivwuInstructionNode.ParseCodeOe;
                case "divwuo.": return DivwuInstructionNode.ParseCodeOeRc;

                // Mulhwx
                case "mulhw": return MulhwInstructionNode.ParseCode;
                case "mulhw.": return MulhwInstructionNode.ParseCodeRc;

                // Mulhwux
                case "mulhwu": return MulhwuInstructionNode.ParseCode;
                case "mulhwu.": return MulhwuInstructionNode.ParseCodeRc;

                // Mulli
                case "mulli": return MulliInstructionNode.ParseCode;

                // Mullwx
                case "mullw": return MullwInstructionNode.ParseCode;
                case "mullw.": return MullwInstructionNode.ParseCodeRc;
                case "mullwo": return MullwInstructionNode.ParseCodeOe;
                case "mullwo.": return MullwInstructionNode.ParseCodeOeRc;

                // Negx
                case "neg": return NegInstructionNode.ParseCode;
                case "neg.": return NegInstructionNode.ParseCodeRc;
                case "nego": return NegInstructionNode.ParseCodeOe;
                case "nego.": return NegInstructionNode.ParseCodeOeRc;

                // Subfx
                case "subf": return SubfInstructionNode.ParseCode;
                case "subf.": return SubfInstructionNode.ParseCodeRc;
                case "subfo": return SubfInstructionNode.ParseCodeOe;
                case "subfo.": return SubfInstructionNode.ParseCodeOeRc;

                // Subx (equivalent to Subf with swaped rA and rB)
                case "sub": return SubInstructionNode.ParseCode;
                case "sub.": return SubInstructionNode.ParseCodeRc;
                case "subo": return SubInstructionNode.ParseCodeOe;
                case "subo.": return SubInstructionNode.ParseCodeOeRc;

                // Subfcx
                case "subfc": return SubfcInstructionNode.ParseCode;
                case "subfc.": return SubfcInstructionNode.ParseCodeRc;
                case "subfco": return SubfcInstructionNode.ParseCodeOe;
                case "subfco.": return SubfcInstructionNode.ParseCodeOeRc;

                // Subcx (equivalent to Subfc with swaped rA and rB)
                case "subc": return SubcInstrucionNode.ParseCode;
                case "subc.": return SubcInstrucionNode.ParseCodeRc;
                case "subco": return SubcInstrucionNode.ParseCodeOe;
                case "subco.": return SubcInstrucionNode.ParseCodeOeRc;

                // Subfe
                case "subfe": return SubfeInstructionNode.ParseCode;
                case "subfe.": return SubfeInstructionNode.ParseCodeRc;
                case "subfeo": return SubfeInstructionNode.ParseCodeOe;
                case "subfeo.": return SubfeInstructionNode.ParseCodeOeRc;

                // Subfic
                case "subfic": return SubficInstructionNode.ParseCode;

                // Subme
                case "subfme": return SubfmeInstructionNode.ParseCode;
                case "subfme.": return SubfmeInstructionNode.ParseCodeRc;
                case "subfmeo": return SubfmeInstructionNode.ParseCodeOe;
                case "subfmeo.": return SubfmeInstructionNode.ParseCodeOeRc;

                // Subze
                case "subfze": return SubfzeInstructionNode.ParseCode;
                case "subfze.": return SubfzeInstructionNode.ParseCodeRc;
                case "subfzeo": return SubfzeInstructionNode.ParseCodeOe;
                case "subfzeo.": return SubfzeInstructionNode.ParseCodeOeRc;

            }

            return null;
        }
    }

對不起,有大量的代碼,但是您可以一遍又一遍地看到它基本上是同一回事,但是每個類都有差異,通常在同一類別中,它們僅相差1或2個常數值。

因此,由於存在大量的復制粘貼,並且一遍又一遍地重復使用幾乎相同的類,因此我嘗試找到方法使其中的一些更加無縫。

因此,為什么我認為自動返回自身會很好,因為我不必每次都花一些時間來重命名復制粘貼; P

不,不是。

當然不如new ThisClass() 但是,如果您要返回基類, 可以避免這種情況(由於下面提到的原因,這並不是一個好主意,但是可以):

public static BaseClass ParseCode(string data)
{
    ...
    string declaringType = MethodBase.GetCurrentMethod().DeclaringType; 
    return (BaseClass)Activator.CreateInstance(declaringType);
}

請注意,與僅手動調用構造函數相比,這是瘋狂的昂貴的操作,並且丟失了一些類型安全性,但是它將使您進行復制粘貼。

訣竅是使用反射來獲取聲明類型(第一行),然后使用Activator.CreateInstance對該類型調用構造函數。 因為返回object您必須將其BaseClassBaseClass

使用CreateInstance重載將參數傳遞給構造函數MSDN 它是相同的想法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM