lundi 14 septembre 2015

c# access static function's variables by name

I am trying to access variables in my static function by name. I know that you can access variables of a class using reflection in a following way:

    public static object GetPropertyValue(object SourceData, string propName)
    {
        return SourceData.GetType().GetProperty(propName).GetValue(SourceData, null);
    }

However I don't know how to access them when variables are located only inside my function:

    private static string LoopThroughRows()
    {
        string sResult = string.Empty;
        var _bindings = IP_DeserializeObject.IP_DeserializeExcelBindings();
        int CompanyID, TaxCode, AccountCode, Amount, DueDate, InvoiceID, VAT_Percent, VAT_Value = -1;

        //list of all column names
        string[] sVariables = new string[]
        {
            "CompanyID",
            "TaxCode",
            "AccountCode",
            "Amount",
            "DueDate",
            "InvoiceID",
            "VAT_Percent",
            "VAT_Value"
        };

        //loop through variables and assing value to them
        foreach (var item in sVariables)
        {

        }
    }

What I will do in a foreach loop, is to assing values to them from my other class _bindings (using reflection), which looks like following (variable names are exactly the same):

public class IP_Bindings : ViewModelBase
{
    IP_Bindings backup;

    private string _CompanyID;
    public string CompanyID
    {
        get { return _CompanyID; }
        set
        {
            _CompanyID = value;
            OnPropertyChanged("CompanyID");
        }
    }

    private string _TaxCode;
    public string TaxCode
    {
        get { return _TaxCode; }
        set
        {
            _TaxCode = value;
            OnPropertyChanged("TaxCode");
        }
    }

    private string _AccountCode;
    public string AccountCode
    {
        get { return _AccountCode; }
        set
        {
            _AccountCode = value;
            OnPropertyChanged("AccountCode");
        }
    }

    private string _Amount;
    public string Amount
    {
        get { return _Amount; }
        set
        {
            _Amount = value;
            OnPropertyChanged("Amount");
        }
    }

    private string _DueDate;
    public string DueDate
    {
        get { return _DueDate; }
        set
        {
            _DueDate = value;
            OnPropertyChanged("DueDate");
        }
    }

    private string _InvoiceID;
    public string InvoiceID
    {
        get { return _InvoiceID; }
        set
        {
            _InvoiceID = value;
            OnPropertyChanged("InvoiceID");
        }
    }

    private string _VAT_Percent;
    public string VAT_Percent
    {
        get { return _VAT_Percent; }
        set
        {
            _VAT_Percent = value;
            OnPropertyChanged("VAT_Percent");
        }
    }

    private string _VAT_Value;
    public string VAT_Value
    {
        get { return _VAT_Value; }
        set
        {
            _VAT_Value = value;
            OnPropertyChanged("VAT_Value");
        }
    }


    //path where we store temporary Excel Template
    private string _sExcelTemplate;
    public string sExcelTemplate
    {
        get { return _sExcelTemplate; }
        set
        {
            _sExcelTemplate = value;
            OnPropertyChanged("sExcelTemplate");
        }
    }



}





Aucun commentaire:

Enregistrer un commentaire