lundi 25 janvier 2016

Getting rid of objects after using reflection

I've been trying to call a method with a string, but one side-effect is that a new object is created everytime I press the button. How can I get rid of this? I've tried using null, but no luck.

First attempt:

string methodName = cboOriginal.Text + "To" + cboConverted.Text;
Type numeralType = typeof(NumeralSystemConversion);
            ConstructorInfo numeralConstructor = numeralType.GetConstructor(Type.EmptyTypes);
            object numeralObject = numeralConstructor.Invoke(new object[] { });

        MethodInfo numeralMethod = numeralType.GetMethod(methodName);
        object numeralValue = numeralMethod.Invoke(numeralObject, new object[] { txtOriginal.Text });

        txtConverted.Text = numeralValue.ToString();

        numeralType = null; numeralConstructor = null; numeralObject = null;
        numeralMethod = null; numeralValue = null;

Second attempt:

string methodName = cboOriginal.Text + "To" + cboConverted.Text;
    convert = typeof(NumeralSystemConversion).GetMethod(methodName).Invoke(typeof(NumeralSystemConversion).GetConstructor(Type.EmptyTypes).Invoke(new object[] { }), new object[] { txtOriginal.Text });
            txtConverted.Text = convert.ToString();
            convert = null;

The 'convert' object is created when the app starts.





Aucun commentaire:

Enregistrer un commentaire