出于日志记录的目的,我想调用.ToString()object []数组中每个对象的方法.我怎样才能以最简单的方式做到这一点?
说我有:
myArray = new Object[]{"astring",1, Customer}
Log(????);
Run Code Online (Sandbox Code Playgroud)
我怎样才能传递一个字符串,例如它的值等于:
"astring".ToString()+1.ToString()+Customer.ToString()
Run Code Online (Sandbox Code Playgroud)
或者更好,每个值之间使用逗号.
我一直在努力装饰+接口。说我有以下“行为”界面:
interface IFlyable { void Fly();}
interface ISwimmable { void Swim();}
Run Code Online (Sandbox Code Playgroud)
主界面
interface IMainComponent { void DoSomethingA(); void DoSomethingB();}
Run Code Online (Sandbox Code Playgroud)
主界面上的装饰器
public class Decorator : IMainComponent
{
private readonly IMainComponent decorated;
[..]
public virtual void DoSomethingA()
{
decorated.DoSomethingA();
}
public virtual void DoSomethingB()
{
decorated.DoSomethingB();
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是如何将装饰对象实现的所有接口转发给装饰器。一个解决方案是使装饰器实现接口:
public class Decorator : IMainComponent, IFlyable, ISwimmable
{
[..]
public virtual void Fly()
{
((IFlyable)decorated).Fly();
}
public virtual void Swim()
{
((ISwimmable)decorated).Swim();
}
Run Code Online (Sandbox Code Playgroud)
但我不喜欢它,因为:
另一种解决方案是添加“手动投射”以传播装饰树:
public class Decorator : IMainComponent
{ …Run Code Online (Sandbox Code Playgroud) 有没有一种简单的方法可以在运行时打印委托的代码?(“包含一种方法”)。
public delegate void SimpleDelegate();
SimpleDelegate delegateInstance =
delegate { DoSomeStuff(); int i = DoOtherStuff() };
Run Code Online (Sandbox Code Playgroud)
现在,我想在屏幕上显示 delegateInstance 的主体。也就是说,做一些类似反射器的事情。我可以这样做吗?也许我可以使用一些 .pdb 文件?
当有一些cpu时间可用时,我会执行几个低限制任务.如果正在运行其他更多导入任务,我不希望执行此任务.即如果出现正常/高优先级任务,我希望低重要性任务暂停,直到重要性任务完成.
有很多低重要性的任务要执行(50到1000).所以我不想为每个任务创建一个线程.但是我相信线程池不允许一些优先级规范,是吗?
你会怎么解决这个问题?
我知道你不能在界面中有一个构造函数,但这是我想要做的:
interface ISomething
{
void FillWithDataRow(DataRow)
}
class FooClass<T> where T : ISomething , new()
{
void BarMethod(DataRow row)
{
T t = new T()
t.FillWithDataRow(row);
}
}
Run Code Online (Sandbox Code Playgroud)
我真的想以某种方式用构造函数替换ISomething's FillWithDataRow方法.
这样,我的成员类可以实现接口,仍然是只读(它不能与FillWithDataRow方法).
有没有人有一个可以做我想要的模式?
我有一个生产者通过爆发产生整数(在几秒钟内产生1到50).我有一个消费者按块使用这些整数.
我希望消费者在制作人完成他的爆发时开始消费(我没有制作人的领先优势,我只知道它已经完成生产,当没有任何产生5秒).
我想到了两种不同的方式:
第一:使用一种不同于另一种消费者的消费者:
private readonly List<int> _ids = new List<int>();
private readonly ManualResetEvent _mainWaiter = new ManualResetEvent(false);
private readonly ManualResetEvent _secondaryWaiter = new ManualResetEvent(false);
//This methods consumes the id from the producer
public void OnConsumeId(int newId)
{
lock(_ids)
{
_ids.Add(newId);
_mainWaiter.Set();
_secondaryWaiter.Set();
}
}
//This methods runs on the dedicated thread :
public void ConsumerIdByBlock()
{
while(true)
{
_mainWaiter.Wait();
while(_secondaryWaiter.Wait(5000));
List<int> localIds;
lock(_ids)
{
localIds = new List<int>(_ids);
_ids.Clear();
}
//Do the job with localIds
}
}
Run Code Online (Sandbox Code Playgroud)
第二:为最后一次更新提供一种令牌
//This …Run Code Online (Sandbox Code Playgroud) 我在里面定义了一个带有表格布局面板的自定义面板.但是,当我在winform上使用此控件时,我无法访问表格布局面板属性.(我希望例如在单元格中添加列或停靠其他控件).我尝试将修饰符属性更改为public,但它仍然无效.如何查看和更改面板布局属性,我该怎么办?
实际上,这个问题可能更通用:如何访问/修改/移动自定义用户控件中包含的控件?
谢谢
我有一本约有一百万件物品的字典.我不断循环抛出词典:
public void DoAllJobs()
{
foreach (KeyValuePair<uint, BusinessObject> p in _dictionnary)
{
if(p.Value.MustDoJob)
p.Value.DoJob();
}
}
Run Code Online (Sandbox Code Playgroud)
执行有点长,大约600毫秒,我想要deacrese它.这是约束:
我想做以下事情:
我没有实现任何先前的想法,因为它可能是很多工作,我想先调查其他选项.所以...任何想法?
我在C#中有一个泛型类,如下所示:
public class GenericClass<T> { ... }
Run Code Online (Sandbox Code Playgroud)
现在,我有Type一个目标对象,并希望,通过反射或其他方式获得Type的对象GenericClass<T>,其中T对应于该类型的对象我有我的目标.
像这样:
Type requiredT = myobject.GetType();
Type wantedType = typeof(GenericClass<requiredT>);
Run Code Online (Sandbox Code Playgroud)
显然这种语法不起作用,但我该怎么办呢?
c# ×10
generics ×2
.net-3.5 ×1
c#-3.0 ×1
delegates ×1
dictionary ×1
ienumerable ×1
interface ×1
linq ×1
parent-child ×1
performance ×1
reflection ×1
string ×1
threadpool ×1
types ×1
winforms ×1