C#中的隐式转换而不创建新对象
Implicit conversion in C# without creating new object
我正在尝试为对象实现一些隐式转换。目前我正在这样做:
public class PathSelector
{
private string _path = null;
public string Path
{
get { return _path; }
set
{
if (value != _path)
{
_path = value;
OnPropertyChanged("Path");
}
}
}
static public implicit operator string(PathSelector pathSelector)
{
return pathSelector.Path;
}
static public implicit operator PathSelector(string path)
{
return new PathSelector() { Path = path };
}
}
正如您在从 String
到 PathSelector
的转换中看到的,我正在生成一个新的 PathSelector
对象。
我是这样使用的:
public PathSelector PluginPathSelector { get; set; } = new PathSelector();
public string PluginPath
{
get
{
return PluginPathSelector;
}
set
{
PluginPathSelector = value;
}
}
我不喜欢这个解决方案的地方是,当我将字符串分配给 PathSelector
对象时,我总是创建一个新对象。这也意味着,在 PathSelector
属性 中需要一个 set
部分。我想将 string
分配给已经创建的对象。有没有办法实现这个?
终于明白你想干什么了
你想要这个:
x.PluginPathSelector = "some string";
直接更改x.PluginPathSelector
中现有对象的路径属性,而不是构造一个新的PathSelector
实例并赋值给x.PluginPathSelector
。
换句话说,你想要这个:
x.PluginPathSelector = "some string";
静默处理,就像您写的一样:
x.PluginPathSelector.Path = "some string";
但是从静态转换运算符内部:
static public implicit operator PathSelector(string path)
而没有,这个做不到,因为这是一个转换运算符。
这条语句:
x.PluginPathSelector = "some string";
是这样处理的:
- 首先将
"some string"
转换成PathSelector
(通过转换运算符)
- 将新对象分配给 属性
转换运算符实现无法到达或了解它 returns 对象的目标,无论是 属性 还是变量或其他什么。
所以没有。这是做不到的。
如果您想避免一直构建新实例,则必须自己手动进行更改。
x.PluginPathSelector.Path = "some string";
为了完整性和作为一种黑客方式,您可以使用丑陋的方式来处理:
- 从 DynamicClass
继承包含 PathSelector 属性 的 class
- 覆盖 TrySetMember 函数
- 处理转换并在该函数中设置路径 属性
我正在尝试为对象实现一些隐式转换。目前我正在这样做:
public class PathSelector
{
private string _path = null;
public string Path
{
get { return _path; }
set
{
if (value != _path)
{
_path = value;
OnPropertyChanged("Path");
}
}
}
static public implicit operator string(PathSelector pathSelector)
{
return pathSelector.Path;
}
static public implicit operator PathSelector(string path)
{
return new PathSelector() { Path = path };
}
}
正如您在从 String
到 PathSelector
的转换中看到的,我正在生成一个新的 PathSelector
对象。
我是这样使用的:
public PathSelector PluginPathSelector { get; set; } = new PathSelector();
public string PluginPath
{
get
{
return PluginPathSelector;
}
set
{
PluginPathSelector = value;
}
}
我不喜欢这个解决方案的地方是,当我将字符串分配给 PathSelector
对象时,我总是创建一个新对象。这也意味着,在 PathSelector
属性 中需要一个 set
部分。我想将 string
分配给已经创建的对象。有没有办法实现这个?
终于明白你想干什么了
你想要这个:
x.PluginPathSelector = "some string";
直接更改x.PluginPathSelector
中现有对象的路径属性,而不是构造一个新的PathSelector
实例并赋值给x.PluginPathSelector
。
换句话说,你想要这个:
x.PluginPathSelector = "some string";
静默处理,就像您写的一样:
x.PluginPathSelector.Path = "some string";
但是从静态转换运算符内部:
static public implicit operator PathSelector(string path)
而没有,这个做不到,因为这是一个转换运算符。
这条语句:
x.PluginPathSelector = "some string";
是这样处理的:
- 首先将
"some string"
转换成PathSelector
(通过转换运算符) - 将新对象分配给 属性
转换运算符实现无法到达或了解它 returns 对象的目标,无论是 属性 还是变量或其他什么。
所以没有。这是做不到的。
如果您想避免一直构建新实例,则必须自己手动进行更改。
x.PluginPathSelector.Path = "some string";
为了完整性和作为一种黑客方式,您可以使用丑陋的方式来处理:
- 从 DynamicClass 继承包含 PathSelector 属性 的 class
- 覆盖 TrySetMember 函数
- 处理转换并在该函数中设置路径 属性