我会假设有一个简单的LINQ查询来执行此操作,我只是不确定如何.请参阅下面的代码段.
class Program
{
static void Main(string[] args)
{
List<Person> peopleList1 = new List<Person>();
peopleList1.Add(new Person() { ID = 1 });
peopleList1.Add(new Person() { ID = 2 });
peopleList1.Add(new Person() { ID = 3 });
List<Person> peopleList2 = new List<Person>();
peopleList2.Add(new Person() { ID = 1 });
peopleList2.Add(new Person() { ID = 2 });
peopleList2.Add(new Person() { ID = 3 });
peopleList2.Add(new Person() { ID = 4 });
peopleList2.Add(new Person() { ID = 5 });
}
}
class Person
{ …
Run Code Online (Sandbox Code Playgroud) 我一直在阅读战略模式,并有一个问题.我在下面实现了一个非常基本的控制台应用程序来解释我在问什么.
I have read that having 'switch' statements is a red flag when implementing the strategy pattern. However, I can't seem to get away from having a switch statement in this example. Am I missing something? I was able to remove the logic from the Pencil, but my Main has a switch statement in it now. I understand that I could easily create a new TriangleDrawer class, and wouldn't have to open the Pencil class, which is good. However, …
c# design-patterns dependency-injection coding-style strategy-pattern
我正在阅读"清洁代码"这本书,并且正在努力解决这个问题.在讨论对象和数据结构时,它指出以下内容:
所以,我从中获得的是我不应该在我的对象上有任何公共属性,我应该只有对属性执行操作的方法.如果我确实需要访问属性,它们应该在数据结构上,可以从我对象上的方法返回?使用这种方法,似乎我需要为我的对象上的Height属性使用GetHeight()和SetHeight()方法,而不是仅使用属性的get和set.
也许我不明白究竟是什么建议,但这是我对" 对象隐藏他们的数据 "的理解.如果你能帮助我理解这一点,我将非常感激!
提前致谢!
我有一个Crate对象,它有一个KeyValuePairs列表.目前,我正在遍历每一对,以查看列表中所有项目的kvp.Value.PixelsWide是否相同.如果是,则返回true,否则返回false.
我现有的方法如下所示:
public bool Validate(Crate crate)
{
int firstSectionWidth = 0;
foreach (KeyValuePair<string, SectionConfiguration> kvp in crate.Sections)
{
if (firstSectionWidth == 0)//first time in loop
{
firstSectionWidth = kvp.Value.PixelsWide;
}
else //not the first time in loop
{
if (kvp.Value.PixelsWide != firstSectionWidth)
{
return false;
}
}
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
我很好奇是否可以在LINQ查询中执行?
在此先感谢您的帮助!
我已经越来越熟悉工厂模式(以及战略模式)以及模式可以带来的巨大好处.但是,我一直在努力应对以下情况:
以前,我会做类似以下的事情,其中有一个经理类可以构建和保存汽车.这里没有依赖注入,并且是一个糟糕的实现,特别是在尝试单元测试时.
public class CarManager
{
public static Car GetCarFromDatabase(int carId) { return new Car(); }
public static void SaveCar(Car car) { }
}
Run Code Online (Sandbox Code Playgroud)
我现在看到我可以Factories
为我制造不同的车,无论是来自数据库,还是来自哪里!这很棒!所以,这是我的问题:
Q1:我的理解是,Factories
只应构建对象,这是正确的吗?如果是这样,我的第二个问题呢?
Q2:如果我按照工厂模式构建我的对象,我应该如何保存我的对象?这有不同的模式,还是我不完全理解工厂模式?
c# design-patterns dependency-injection strategy-pattern factory-pattern
我一直在玩XAML中声明对象.我在Silverlight程序集中有这些类:
public class TextItem
{
public string TheValue { get; set; }
}
public class TextItemCollection
{
public ObservableCollection<TextItem> TextItems { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后,我在我的XAML中有这个:
<UserControl.Resources>
<app:TextItemCollection x:Key="TextItemsResource">
<app:TextItemCollection.TextItems>
<app:TextItem TheValue="Hello world I am one of the text values"/>
<app:TextItem TheValue="And I am another one of those text items"/>
<app:TextItem TheValue="And I am yet a third!"/>
</app:TextItemCollection.TextItems>
</app:TextItemCollection>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)
出于某种原因,如果我在尝试调试应用程序时包含该节点,Silverlight会挂起(我只看到旋转的蓝色加载圆圈).如果我注释掉该节点,它会立即运行.
有任何想法吗?
我正在尝试创建一个非常基本的HTML5页面来抓取地理位置,但我收到了一个错误.
这是我在'script'标签中的内容:
function GetGeo() {
if (!navigator.geolocation) {
alert("Could not find geolocation");
}
else {
navigator.geolocation.getCurrentPosition(showMap, function (error) { alert("error encountered: " + error.message ); });
}
}
function showMap(position) {
window.alert(position.coords.latitude + ", " + position.coords.longitude);
}
Run Code Online (Sandbox Code Playgroud)
页面上只有一个按钮,调用GetGeo()函数.
我做的是将.html文件放到我手机的SD卡上.然后,我浏览了"content://com.android.htmlfileprovider/sdcard/GetGeo.html",页面显示正确.当我单击按钮并执行时,我收到以下错误:"最后一个位置提供程序已被禁用"
我有几个问题:1:将SD文件中的html文件丢弃并尝试在那里运行是不对的?2:有没有什么基本的我做错了,不让它从移动浏览器获得地理位置?
在此先感谢您的帮助!
我已经在我想要做的事情下面放了一些非常基本的代码.我有'DoSomethingAshnc'方法执行异步操作.我希望'DoSomething'方法是一个不接受action参数并返回int的同步方法.
public void DoSomething(Action<int> actionToPerformOnComplete)
{
DoSomethingAsync(delegate(int val)
{
actionToPerformOnComplete(val);
});
}
Run Code Online (Sandbox Code Playgroud)
是否甚至可以让'DoSomething'返回一个整数,就像方法同步发生一样?
我已将下面的代码简化为一个基本示例,但我仍然无法获得设置的值.执行propertyInfo.SetValue()时,它将在我的Contact对象的setter上命中一个断点,并在'setter'中正确设置该值.但是,在执行SetValue()之后,projectContact.Contact对象上的字符串属性尚未更改为"a".知道我在这里做错了什么吗?
IEnumerable<ProjectContact> contacts = GetContactsByProject(projectId);
foreach (ProjectContact projectContact in contacts)
{
foreach (PropertyInfo propertyInfo in projectContact.Contact.GetType().GetProperties())
{
if (propertyInfo.PropertyType == typeof(string))
{
propertyInfo.SetValue(projectContact.Contact, "a", null);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个项目,可以添加元素到AutoCad绘图.我注意到我开始在多种方法中编写相同的十行代码(为简单起见仅显示两行).
初步实施: 您会注意到唯一真正改变的是添加一个Line而不是Circle.
[CommandMethod("Test", CommandFlags.Session)]
public void Test()
{
AddLineToDrawing();
AddCircleToDrawing();
}
private void AddLineToDrawing()
{
using (DocumentLock lockedDocument = Application.DocumentManager.MdiActiveDocument.LockDocument())
{
using (Database database = Application.DocumentManager.MdiActiveDocument.Database)
{
using (Transaction transaction = database.TransactionManager.StartTransaction())//Start the transaction
{
//Open the block table for read
BlockTable blockTable = transaction.GetObject(database.BlockTableId, OpenMode.ForRead) as BlockTable;
//Open the block table record model space for write
BlockTableRecord blockTableRecord = (BlockTableRecord)transaction.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
Line line = new Line(new Point3d(0, 0, 0), new Point3d(10, 10, 0));
blockTableRecord.AppendEntity(line);
transaction.AddNewlyCreatedDBObject(line, true);
transaction.Commit();
} …
Run Code Online (Sandbox Code Playgroud) c# ×8
coding-style ×2
linq ×2
properties ×2
.net-3.5 ×1
abstraction ×1
android ×1
asynchronous ×1
autocad ×1
delegates ×1
foreach ×1
geolocation ×1
html5 ×1
mobile ×1
object ×1
reflection ×1
silverlight ×1
xaml ×1