lundi 28 novembre 2016

Passing variable type to generic method

I have a generic method :

public static T GetSetting<T>(string Key){
   ....
}

Which returns a setting value from database. Now I'm trying to make it fill the class automatically with Settings:

    public static T GetAllSettings<T>(this T m)
            {
                FieldInfo[] fields = typeof(T).GetFields(BindingFlags.Public);
                foreach(FieldInfo f in fields)
                   f.SetValue(m, User.GetSetting<f.FieldType>(f.Name), null);
//                                               ~ <=Error in here      
            }

But I'm getting the error:

'f' is a variable but is used like a type

Well I'm actually getting the f FieldType and not the f itself





Aucun commentaire:

Enregistrer un commentaire