我需要使用模式工厂的想法将我的Person类实体中的实体属性Address与我的FactoryEntities类中的表达式linq相关联,看看这就是我拥有的和我想做的事情:
Address address = new Address();
address.Country = "Chile";
address.City = "Santiago";
address.ZipCode = "43532";
//Factory instance creation object
//This is idea
Person person = new FactoryEntity<Person>().AssociateWithEntity(p=>p.Address, address);
public class Person: Entity
{
public string Name{ get; set; }
public string LastName{ get; set; }
public Address Address{ get; set; }
}
public class Address: Entity
{
public string Country{ get; set; }
public string City{ get; set; }
public string ZipCode{ get; set; }
}
public class FactoryEntity<TEntity> where …Run Code Online (Sandbox Code Playgroud) 这让我感到困惑,也许有人可以用我的无知来照亮教育之光.这是在C#windows应用程序中.我从一个线程访问列表框的内容.当我尝试像这样访问它
prgAll.Maximum = lbFolders.SelectedItems.Count;Run Code Online (Sandbox Code Playgroud)
我收到了错误.但是,这是我没有得到的部分.如果我注释掉那一行,那就是下一行foreach (string dir in lbFolders.SelectedItems)Run Code Online (Sandbox Code Playgroud)
执行得很好.
编辑:像往常一样,我缺乏沟通技巧.让我澄清一下.
我知道从除了创建它们之外的线程访问GUI项会导致问题.我知道访问它们的正确方法是通过委托.
我的问题主要在于:为什么我可以正常访问和迭代SelectedItems对象,但是当我尝试获取(未设置)它的Count属性时,它会爆炸.
此代码以多种方式执行.当它由表单按钮执行时它工作(按钮启动一个线程,在循环中它调用这个方法=它工作).但是当我从表单中的BackgroundWorker调用该方法时,它不起作用.
使用以下代码:
private void resizeThreadSafe(int width, int height)
{
if (this.form.InvokeRequired)
{
this.form.Invoke(new DelegateSize(resizeThreadSafe),
new object[] { width, height });
}
this.form.Size = new Size(width, height); // problem occurs on this line
this.form.Location = new Point(0, 0); // dummy coordinate
}
Run Code Online (Sandbox Code Playgroud)
然后在包含this.form.Size = ...我的行上得到以下异常:
InvalidOperationException was unhandled
Cross-thread operation not valid: Control 'Form1' accessed from a thread other
than the thread it was created on.
Run Code Online (Sandbox Code Playgroud)
为什么?