方法调用对象作为返回值 [英] Methodcall with object as return value

查看:77
本文介绍了方法调用对象作为返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我的设置文件管理有点困难。

我读书时很好写作,但我想清理一些代码。



我真的很想读取一个设置项并将其值转换为所需的类型。 />




更好概述的一些例子:



当前状态:

Hello there,

I'm a bit stuck at my Settings-file management.
I'm fine with reading and writing but I'd like to clean up some code.

I'd really like to read a settings- item and convert it's value to the needed type.


Some example for a better overview:

The current state:

 public class Pref
    {
        public Boolean ShowHiddenItems { get; set; }
    }
private void Form1_Load(object sender, EventArgs e)
        {
            Pref p = new Pref();

            p.ShowHiddenItems = (bool)ReadSubSetting(p.ShowHiddenItems, "show=", "show=true");

        }

private static object ReadSubSetting(object variable, String def, String line)
        {
            if (variable is bool)
            {
                if (line.StartsWith(def))
                {
                    line = line.Substring(def.Length,
                        line.Length -
                        def.Length);

                    bool bDummy;
                    Boolean.TryParse(line, out bDummy);

                    return bDummy;
                }
            }

            /* More conversations coming... */

            /* Some dummy return */
            return false;
        }





我的目标:



My goal:

private void Form1_Load(object sender, EventArgs e)
        {
            Pref p = new Pref();

            p.ShowHiddenItems = ReadSubSetting(p.ShowHiddenItems, "show=", "show=true");

        }



但是在尝试这个时,编译器告诉我他不能将对象转换为bool(这没关系)但是直接投射不好看。



无论如何,有没有办法以这种方式实现我的目标甚至是更好的方式?


But when trying this, the compiler tells me that he can not convert an object to a bool (which is okay) but the direct casting is not nice looking.

Anyway, is there a way to accomplish my goal this way or even a better way?

推荐答案

您可以让方法接受泛型类型,并在方法内部进行转换,例如:



You could make the method accept a generic type, and do the casting inside the method, for example like this:

private static T ReadSubSetting<T>(T variable, String def, String line)
{
   if (variable is bool)
   {
       if (line.StartsWith(def))
       {
           line = line.Substring(def.Length, line.Length - def.Length);
			   
            System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(T));
            return (T)converter.ConvertFrom(line);
        }
   }

   /* More conversations coming... */

   /* Some dummy return */
   return default(T);
}





然后您可以将其称为:



And then you can call it as following:

Pref p = new Pref();
p.ShowHiddenItems = ReadSubSetting(p.ShowHiddenItems, "show=", "show=true");





这是假设变量的类型与方法的返回类型相同。



This is assuming the type of the variable is the same as the return type of the method.


您将遇到此策略的问题 - ReadSubSetting 返回 object ,但是你将它设置为bool。







一般的解析器可以像这样工作:





You are going to run into problems with this strategy - ReadSubSetting returns object, but you are setting this to bool.



A generic parser can be made to work like this:


static void Form1_Load(object sender, EventArgs e)
{
    Pref p = new Pref();
    p.ShowHiddenItems = ReadSubSetting<bool>("true");
}

public static T ReadSubSetting<t>(string value)
{
     return (T)TypeDescriptor.GetConverter(typeof(T)).ConvertFromString(value);
}





您可以使用以上作为起点。如果你需要根据类型进行预处理,你可以使用像这样的结构if(typeof(T))== typeof(bool)) ...

注意仅适用于主要类型 [ ^ ] ,但是您可以使用强制转换为非主体创建类似的方法。你的方法可能也遇到字符串问题,但是你开始使用字符串,所以无论如何。



You can use the above as starting points. If you need to pre-process according to the type, you can use a construct like if(typeof(T)) == typeof(bool))...
Note only works with primative types[^], but you can write a similar method for non-primatives using casting. You might also have problems with strings with my method but you are starting with strings, so no matter.


这篇关于方法调用对象作为返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆