cannot implicitly convert System.Type to object

Almis

I am trying to make the generic method for loading the form settings in .NET C# where each setting would contain it's own try catch block (to proceed with other settings when a single one is not valid). However I cannot figure out how to work around the assignment of the appsetting to an object. The comipler does not allow me to implicitly cast type of an object.

private void LoadFormSettings(object o)
{
  try
  {
    //Load settings when application is started
    Type t = o.GetType();

    // Operator '<' cannot be applied to operands of type 'method group' and 'System.Type'
    o = getAppSetting<o.GetType()>("Setting");
    // Cannot implicitly convert type 't' to 'object'
    o = getAppSetting<t>("Setting");
    // The type arguments for method... cannot be inferred from the usage. Try specifying the type arguments explicitly
    o = getAppSetting("Setting");
  }
  catch (Exception ee)
  {
  }
}

private T getAppSetting<T>(string key)
{
  string value = config.AppSettings.Settings[key].Value;
  if (typeof(T) == typeof(Point))
  {
    string[] values = value.Split(',');
    return (T) Convert.ChangeType(value, typeof(T));
  }
}
qxg

Type is type and t is an instance. Generic requires type instead of instance. You can only write F<Type>() instead of F<t>(). In your case it's better write

Type t = o.GetType();
o = getAppSetting("Setting", t);

object getAppSetting(string key, Type t)
{
  string value = config.AppSettings.Settings[key].Value;
  if (t == typeof(Point))
  {
    string[] values = value.Split(',');
    return Convert.ChangeType(value, t);
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cannot implicitly convert type long to a Object Model

From Dev

cannot implicitly convert type void to object

From Dev

"Cannot implicitly convert type 'bool[]' to 'object[]'"

From Dev

Cannot implicitly convert type System.EventHandler to System.EventHandler<object> error

From Dev

SignalR error Cannot implicitly convert type 'System.Threading.Tasks.Task<object>' to 'string'

From Dev

Cannot implicitly convert type 'object' to 'System.Data.SqlClient.SqlParameter'. An explicit conversion exists (are you missing a cast?)

From Dev

Cannot implicitly convert type 'System.Threading.Tasks.Task<object>' to 'string' in SignalR

From Dev

Cannot implicitly convert type 'System.Threading.Tasks.Task<object>' to 'string' in SignalR

From Dev

Cannot implicitly convert type 'frmRemote.RemoteClickHandler' to 'System.EventHandler'

From Dev

Cannot implicitly convert type to 'System.Collections.Generic.List

From Dev

Cannot implicitly convert type 'void' to 'System.Net.CookieContainer'

From Dev

Cannot implicitly convert type 'void' to System.Windows.Forms.MouseEventHandler

From Dev

Cannot implicitly convert type 'System.Linq.IQueryable?

From Dev

Cannot implicitly convert type 'void' to 'System.Threading.Tasks.Task'

From Dev

Cannot implicitly convert type 'int' to 'System.Data.DataTable'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List

From Dev

Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox'

From Dev

Cannot implicitly convert to type string to System.Net.Mail.MailAddress

From Dev

Cannot implicitly convert type System.Collections.Generic.List

From Dev

Cannot implicitly convert type ('string', 'string') to System.Net.ICredentialsByHost

From Dev

Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox'

From Dev

Cannot implicitly convert type 'System.Linq.IQueryable

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List' to 'string'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable

From Dev

Cannot implicitly convert type 'frmRemote.RemoteClickHandler' to 'System.EventHandler'

From Dev

Cannot implicitly convert type 'bool' to 'System.Threading.Tasks.Task'

From Dev

Error Cannot implicitly convert type 'string' to 'System.DateTime' on return

From Dev

Cannot implicitly convert type 'System.Linq.IQueryable<char[]>' to 'string[]'

From Dev

Cannot implicitly convert type string to System.Drawing.Color

Related Related

  1. 1

    Cannot implicitly convert type long to a Object Model

  2. 2

    cannot implicitly convert type void to object

  3. 3

    "Cannot implicitly convert type 'bool[]' to 'object[]'"

  4. 4

    Cannot implicitly convert type System.EventHandler to System.EventHandler<object> error

  5. 5

    SignalR error Cannot implicitly convert type 'System.Threading.Tasks.Task<object>' to 'string'

  6. 6

    Cannot implicitly convert type 'object' to 'System.Data.SqlClient.SqlParameter'. An explicit conversion exists (are you missing a cast?)

  7. 7

    Cannot implicitly convert type 'System.Threading.Tasks.Task<object>' to 'string' in SignalR

  8. 8

    Cannot implicitly convert type 'System.Threading.Tasks.Task<object>' to 'string' in SignalR

  9. 9

    Cannot implicitly convert type 'frmRemote.RemoteClickHandler' to 'System.EventHandler'

  10. 10

    Cannot implicitly convert type to 'System.Collections.Generic.List

  11. 11

    Cannot implicitly convert type 'void' to 'System.Net.CookieContainer'

  12. 12

    Cannot implicitly convert type 'void' to System.Windows.Forms.MouseEventHandler

  13. 13

    Cannot implicitly convert type 'System.Linq.IQueryable?

  14. 14

    Cannot implicitly convert type 'void' to 'System.Threading.Tasks.Task'

  15. 15

    Cannot implicitly convert type 'int' to 'System.Data.DataTable'

  16. 16

    Cannot implicitly convert type 'System.Collections.Generic.List

  17. 17

    Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox'

  18. 18

    Cannot implicitly convert to type string to System.Net.Mail.MailAddress

  19. 19

    Cannot implicitly convert type System.Collections.Generic.List

  20. 20

    Cannot implicitly convert type ('string', 'string') to System.Net.ICredentialsByHost

  21. 21

    Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox'

  22. 22

    Cannot implicitly convert type 'System.Linq.IQueryable

  23. 23

    Cannot implicitly convert type 'System.Collections.Generic.List' to 'string'

  24. 24

    Cannot implicitly convert type 'System.Collections.Generic.IEnumerable

  25. 25

    Cannot implicitly convert type 'frmRemote.RemoteClickHandler' to 'System.EventHandler'

  26. 26

    Cannot implicitly convert type 'bool' to 'System.Threading.Tasks.Task'

  27. 27

    Error Cannot implicitly convert type 'string' to 'System.DateTime' on return

  28. 28

    Cannot implicitly convert type 'System.Linq.IQueryable<char[]>' to 'string[]'

  29. 29

    Cannot implicitly convert type string to System.Drawing.Color

HotTag

Archive