在 C# 中转换对象引用

发布于 2024-10-23 18:39:10 字数 344 浏览 2 评论 0原文

如果我有一个函数引用一个对象。如何转换参数以避免类型不匹配?

Dictionary<string, string> mySettings = new Dictionary<string, string>();
.
.
saveSettings(ref mySettings);
.
.
void saveSettings(ref object)
{
}

对 saveSettings 的调用会导致以下错误消息:

无法从“ref Dictionary”转换为“ref object”

我不是在寻找解决方法,我已经这样做了,我想知道这种直接方法是否可行。

If I have a function that takes a reference to an object. How do I cast the parameter to avoid a type mismatch?

Dictionary<string, string> mySettings = new Dictionary<string, string>();
.
.
saveSettings(ref mySettings);
.
.
void saveSettings(ref object)
{
}

The call to saveSettings results in the following error message:

cannot convert from 'ref Dictionary' to 'ref object'

I'm not looking for a workaround, I've done that, I would like to know if this direct approach is possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

那些过往 2024-10-30 18:39:10

如果保存设置采用多种类型,为什么不使用通用类型呢?特别是如果它们共享一个通用接口。

void saveSettings<T>(T obj);

If save settings is taking multiple types, why not use a generic instead? Esp if they share a common interface.

void saveSettings<T>(T obj);
枫以 2024-10-30 18:39:10
var objectMySettings = mySettings as object;
if (objectMySettings != null)
    saveSettings(ref objectMySettings);
var objectMySettings = mySettings as object;
if (objectMySettings != null)
    saveSettings(ref objectMySettings);
倦话 2024-10-30 18:39:10
void saveSettings(ref object)

saveSettings 的定义格式错误。您需要在“ref object”后面添加一个标识符。但我会将“ref 对象”改为字典。

void saveSettings(ref object)

The definition of saveSettings is malformed. You need an identifier after "ref object". But I'd make the "ref object" a Dictionary instead.

牵强ㄟ 2024-10-30 18:39:10

这样做:

 protected void Button1_Click(object sender, EventArgs e)
{
    Dictionary<string, string> mySettings = new Dictionary<string, string>();

    saveSettings(mySettings);
}

void saveSettings( Dictionary<string, string> Settings)
{
}

Do it this way:

 protected void Button1_Click(object sender, EventArgs e)
{
    Dictionary<string, string> mySettings = new Dictionary<string, string>();

    saveSettings(mySettings);
}

void saveSettings( Dictionary<string, string> Settings)
{
}
花想c 2024-10-30 18:39:10

像这样的东西:

static void Main()
{

   Dictionary<string, string> mySettings = new Dictionary<string, string>();
   object o = mySettings;
   SaveSettings(ref o);
   // o now has an item.
}

static void SaveSettings(ref object o)
{
    var d = o as Dictionary<string, string>;
    d.Add("Some", "String");
}

编辑一些调试输出。
在此处输入图像描述

Something like this:

static void Main()
{

   Dictionary<string, string> mySettings = new Dictionary<string, string>();
   object o = mySettings;
   SaveSettings(ref o);
   // o now has an item.
}

static void SaveSettings(ref object o)
{
    var d = o as Dictionary<string, string>;
    d.Add("Some", "String");
}

EDIT Some debugging output.
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文