2016-11-29 114 views
0

好吧,我现在可以做到这一点,但我想知道它是否可以作为“自动”属性更加无缝地完成。从静态方法的内部返回类的新实例?

class Test : PPCNode 
    { 

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

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

所以这基本上是每个说的代码。

现在,这段代码发生在很多类中,所以它是一样的,除了实例是它所在的类。

所以这就是我想知道的,是否有可能通过某种汽车的东西,如新的实例。

“return new 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

+0

有办法移动代码,但我不确定你认为应该如何使它更加无缝。也许你想工作的更完整的例子? – BradleyDotNET

+0

当然会给一个更清晰的图片:) – Zerowalker

+0

你是否要求一些泛型静态方法,提供你想要什么类的实例??? – SSH

回答

1

没有,真的没有,

因此,为什么我想回自己,我就不必重命名,每一次复制粘贴,赢得了一些小的时候自动将是很好的。当然不是new ThisClass()那么好。但是,如果你是返回一个基类,你可以不用它(不是真的下文提到的原因是一个好主意,但它的工作原理):

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

注意,这是疯狂的昂贵相比,只是手动调用你的构造函数,并且你失去了一些类型的安全性,但它会让你复制粘贴。

诀窍是使用反射来获取声明类型(第一行),然后使用Activator.CreateInstance来调用该类型的构造函数。因为那返回object你必须将它投射到BaseClass

使用此重载的CreateInstance将参数传递给构造函数:MSDN。它的想法虽然相同。

+0

啊,如果价格昂贵,不值得,有点烂。 虽然很受赞赏的例子。 有可能有一些整洁的方式来做我做的。 谢谢,将接受这个答案,因为它做我要求的:) – Zerowalker

+0

@Zerowalker反射永远不会便宜:) – BradleyDotNET

+0

啊好知道:) – Zerowalker

相关问题