为什么没有设置此属性? [英] Why is this property not getting set?
问题描述
在投射属性后,我难以尝试设置属性的值.我不确定这是否叫做拳击.
I'm having trouble trying to set the value of a property after i cast it. I'm not sure if this is called boxing.
无论如何,将设置新变量,但未设置原始变量.我以为新变量只是原始变量的引用.但是当我检查智能/调试观察器时,原始属性仍然为空.
Anyways, the new variable is getting set, but the original is not. I thought the new variable is just a reference to the original. But when i check the intellisence/debug watcher, the original property is still null.
这是代码.
// NOTE: data is either a Foo || FooFoo || FooBar, at this point.
// only Foo impliments ITagElement.
if (data is ITagElement)
{
var tags = ((ITagElement)data).TagList;
// At this point, tags == null and data.TagList == null.
if (tags.IsNullOrEmpty())
{
tags = new List<Tag>();
}
tags.Add(new Tag
{
K = xmlReader.GetAttribute("k"),
V = xmlReader.GetAttribute("v")
});
// At this point, Tags.Count == 1 and data.TagList == null :( :( :(
}
是否注意到有关 tags
和 data.TagList
的值的内联注释?可以解释一下我做错了什么吗?我以为变量 tags
只是指向属性 data.TagList
..的引用指针,但看起来并非如此.
Notice my inline comments about the values for tags
and data.TagList
? Can some explain what I have done wrong? I thought the variable tags
is just a reference pointer to the property data.TagList
.. but it looks like it's not.
谢谢你们的回答!这是令人尴尬的原因,因为我多年来一直在做这些事情,但仍然忘记/没有注意到像这样的简单事情.(当然,现在我已经见识了,所以引起了很大的议论.)
thanks for the answers guys! it's embarassing cause i've been doing ths stuff for years and still forget/not notice simple things like this. (And of course, makes so much sence now that i see the light).
马克(Marc)得到了要点,因为他的答案(IMO)对于我的单个金发脑细胞来说是最简单的.
Marc got the points because his answer (IMO) was the simplest for my single blond brain cell.
谢谢!
推荐答案
标记和数据是完全隔离的变量.仅仅是因为您将对象分配给 tags
,这与变量 数据
没什么区别.
Tags and data are completely isolated variables. Just because you assign an object to tags
, this makes no difference to the variable data
.
您会感到困惑的是,当两个变量指向同一个 object 时,然后更改为(单个) object 可以通过变量看到.
What you are getting confused is that when the two variables point to the same object, then changes to the (single) object will be seen through either variable.
基本上,如果 data
开始为非空,则您拥有
Basically, if data
starts non-null, you have
- 数据:指向列表A"
- 标签:指向列表A"
在这种情况下,将对象添加到 data
或 tags
实际上是将对象添加到相同的列表A"
In this scenario, adding an object to either data
or tags
is actually adding an object to the same "list A"
但是,如果 data
开始为空,则您有:
However, if data
starts null, you have:
- 数据:指向空
- 标签:指向null
然后您分配标签
,给出:
- 数据:指向空
- 标签:指向列表B"
您必须将列表B"手动分配给 data
使其保持不变.
You would have to manually assign the "list B" to data
to make it stick.
但是,有一种方法可以使它(分配接口/对象)正常工作: ref
和泛型:
There is, however, one way to get it (assigning an interface/object) to work: ref
and generics:
using System.Collections.Generic;
using System;
class Foo : IBar
{
List<string> list = new List<string>();
public int Count { get { return list.Count; } }
void IBar.Add(string s) { list.Add(s); }
}
interface IBar
{
void Add(string s);
}
static class Program
{
static void Main()
{
Foo foo = null; // note foo starts as null
CreateAndAdd(ref foo, "abc");
Console.WriteLine(foo.Count); // prove non-null and added
}
static void CreateAndAdd<T>(ref T data, string s) where T : IBar, new()
{
if (data == null) { data = new T(); } // create
data.Add(s); // mutate
}
}
这篇关于为什么没有设置此属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!