Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 794 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I make the return type of a method generic?

#1
Is there a way to make this method generic so I can return a string, bool, int, or double? Right now, it's returning a string, but if it's able find "true" or "false" as the configuration value, I'd like to return a bool for example.


public static string ConfigSetting(string settingName)
{
return ConfigurationManager.AppSettings[settingName];
}
Reply

#2
You need to make it a generic method, like this:

public static T ConfigSetting<T>(string settingName)
{
return /* code to convert the setting to T... */
}

But the *caller* will have to specify the type they expect. You could then potentially use [`Convert.ChangeType`][1], assuming that all the relevant types are supported:

public static T ConfigSetting<T>(string settingName)
{
object value = ConfigurationManager.AppSettings[settingName];
return (T) Convert.ChangeType(value, typeof(T));
}

I'm not entirely convinced that all this is a good idea, mind you...


[1]:

[To see links please register here]

Reply

#3
You could use `Convert.ChangeType()`:

public static T ConfigSetting<T>(string settingName)
{
return (T)Convert.ChangeType(ConfigurationManager.AppSettings[settingName], typeof(T));
}
Reply

#4
private static T[] prepareArray<T>(T[] arrayToCopy, T value)
{
Array.Copy(arrayToCopy, 1, arrayToCopy, 0, arrayToCopy.Length - 1);
arrayToCopy[arrayToCopy.Length - 1] = value;
return (T[])arrayToCopy;
}

I was performing this throughout my code and wanted a way to put it into a method. I wanted to share this here because I didn't have to use the Convert.ChangeType for my return value. This may not be a best practice but it worked for me. This method takes in an array of generic type and a value to add to the end of the array. The array is then copied with the first value stripped and the value taken into the method is added to the end of the array. The last thing is that I return the generic array.
Reply

#5
There are many ways of doing this(listed by priority, specific to the OP's problem)

1. **Option 1: Straight approach** - Create multiple functions for each type you expect rather than having one generic function.


public static bool ConfigSettingInt(string settingName)
{
return Convert.ToBoolean(ConfigurationManager.AppSettings[settingName]);
}

2. **Option 2: When you don't want to use fancy methods of conversion** - Cast the value to object and then to generic type.

public static T ConfigSetting<T>(string settingName)
{
return (T)(object)ConfigurationManager.AppSettings[settingName];
}
**Note -** This will throw an error if the cast is not valid(your case). I would not recommend doing this if you are not sure about the type casting, rather go for option 3.


3. **Option 3: Generic with type safety** - Create a generic function to handle type conversion.

public static T ConvertValue<T,U>(U value) where U : IConvertible
{
return (T)Convert.ChangeType(value, typeof(T));
}


**Note -** T is the expected type, note the where constraint here(type of U must be IConvertible to save us from the errors)
Reply

#6
You have to convert the type of your return value of the method to the Generic type which you pass to the method during calling.

public static T values<T>()
{
Random random = new Random();
int number = random.Next(1, 4);
return (T)Convert.ChangeType(number, typeof(T));
}


You need pass a type that is type casteable for the value you return through that method.



If you would want to return a value which is not type casteable to the generic type you pass, you might have to alter the code or make sure you pass a type that is casteable for the return value of method. So, this approach is not reccomended.


Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through