我有一个复杂的对象类型,我正在覆盖"base.ToString()"方法,以便它返回一个字符串属性值.
例如:
class foo
{
public string StringValue{get;set;}
public int SomeOtherValue{ get;set;}
public override ToString()
{
return StringValue;
}
}
Run Code Online (Sandbox Code Playgroud)
所以这允许我轻松地使用检索StringValue属性的值.
有没有办法可以覆盖或扩展基本功能,以便我可以使用如下的简单代码来设置StringValue属性?
foo aFoo = new foo();
aFoo = "Some string value";
Run Code Online (Sandbox Code Playgroud) 我认为这个问题可以使用反射进行排序(这是一种我不太确定的技术).
我的代码正在接收一些在运行时序列化为XML的代码对象.当我收到它并将其反序列化时,一个字段让我有些麻烦.
有一个字段可以包含以下数据类的组合(为简洁起见,已简化):
class KeyValuePairType
{
public string Key;
public string Value;
}
class KeyValueListPair
{
public string Key;
public string[] Value;
}
Run Code Online (Sandbox Code Playgroud)
我将这些作为对象[]接收到我的代码中,并且我需要在运行时确定它包含的内容,以便我可以在需要KeyValuePairType []和KeyValueListPair []作为参数的本地对象上调用接口,例如
public DoSomeWork(KeyValuePairType[] data1, KeyValueListPair[] data2)
Run Code Online (Sandbox Code Playgroud)
我有以下案例来应对:
object []包含:
在什么情况下我调用DoSomeWork(null,null);
一个KeyValuePairType数组,在这种情况下我调用DoSomeWork(KeyValuePairType [],null);
一个KeyValueListPair数组,在这种情况下我调用DoSomework(null,KeyValueListPair []);
或者每个的数组,在这种情况下我调用DoSomework(KeyValuePairType [],KeyValueListPair []);
欢迎任何想法.
谢谢
事实证明,对象数组包含随机的离散对象序列.最初,我被引导相信它可能是一系列离散和这些对象的数组.
因为它是LINQ语句将涵盖所有可能性.
我能否对那些回答的人表示非常感谢.我已经为那些回答LINQ语句的人发布了+1.
好吧,我有一系列基于基类的对象,这些对象随机存储在Dictionary对象中.例如
class TypeA
{
public int SomeNumber {get; set;}
public void SomeFunction()
}
class TypeB : TypeA
{
public string SomeString {get; set;}
}
class TypeC : TypeA
{
public bool StuffReady()
}
Dictionary listOfClasses <long, TypeA>;
Run Code Online (Sandbox Code Playgroud)
Key值是已放入字典的对象数的运行计数.它与当前字典计数不匹配.
我希望找到TypeB的一个对象,其SomeString =="123"说,并删除它.这样做的最佳方式是什么?
操作系统:Windows.语言:C
我有一个线程通过TCP向服务器发送请求,并在信号量上无限期等待响应.还有另一个线程读取套接字,将接收到的数据复制到公共缓冲区中,并使信号脉冲,以便从等待中退出.这一切都很好.但是在由于某种原因导致进程被终止的情况下,等待信号量的线程在其他线程终止时等待.
为什么线程等待信号量在进程被杀死时不会结束?我在Linux论坛上看到SEM_UNDO在这种情况下有所帮助.Windows上有类似的东西吗?任何解决方法?
考虑以下类:
class TypeA;
class TypeB : TypeA;
class TypeC : TypeA;
class TypeD : TypeA;
Run Code Online (Sandbox Code Playgroud)
以及List <>类型:
List<TypeB> listTypeB;
List<TypeC> listTypeC;
List<TypeD> listTypeD;
Run Code Online (Sandbox Code Playgroud)
现在,TypeA具有类型为Object1的属性Prop1,我想找到哪个列表中存储了具有给定值的Prop1的项目.有没有办法可以做以下的事情,所以我只需要编写一次搜索代码?
bool LocateInAnyList(Object1 findObj)
{
bool found = false;
found = ContainsProp1(findObj, listTypeB);
if(!found)
{
found = ContainsProp1(findObj, listTypeC);
}
if(!found)
{
found = ContainsProp1(findObj, listTypeD);
}
return found;
}
bool ContainsProp1(Object1 searchFor, List<TypeA> listToSearch)
{
bool found = false;
for(int i = 0; (i < listToSearch.Count) & !found; i++)
{
found = listToSearch[i].Prop1 == searchFor;
} …Run Code Online (Sandbox Code Playgroud) c# ×4
collections ×2
c ×1
hang ×1
inheritance ×1
linq ×1
reflection ×1
semaphore ×1
windows ×1