我正在学习C#.我的意思是关闭 a construct that can adopt the changes in the environment in which it is defined.
示例:
List<Person> gurus =
new List<Person>()
{
new Person{id=1,Name="Jon Skeet"},
new Person{id=2,Name="Marc Gravell"},
new Person{id=3,Name="Lasse"}
};
void FindPersonByID(int id)
{
gurus.FindAll(delegate(Person x) { return x.id == id; });
}
Run Code Online (Sandbox Code Playgroud)
该变量id在FindPersonByID()的范围内声明但是我们仍然可以访问id匿名函数内的局部变量(即)delegate(Person x) { return x.id == id; }
(1)我对封闭的理解是否正确?
(2)我们可以从封闭中获得哪些优势?
我对工厂方法模式的理解是(如果我错了,请纠正我)
工厂方法模式
"工厂方法允许客户端将产品创建(实例创建)委托给子类".
我们可以通过两种方式创建Factory Method模式.
(i)当客户被限制为产品(实例)创建时.
(ii)有多种产品可供使用.但是决定需要退回哪个产品实例.
如果要创建抽象方法模式
示例:
public enum ORMChoice
{
L2SQL,
EFM,
LS,
Sonic
}
//Abstract Product
public interface IProduct
{
void ProductTaken();
}
//Concrete Product
public class LinqtoSql : IProduct
{
public void ProductTaken()
{
Console.WriteLine("OR Mapping Taken:LinqtoSql");
}
}
//concrete product
public class Subsonic : IProduct
{
public void ProductTaken()
{
Console.WriteLine("OR Mapping Taken:Subsonic");
}
}
//concrete product
public class EntityFramework : IProduct
{
public void ProductTaken()
{ …Run Code Online (Sandbox Code Playgroud) 为了我的理解目的,我实施了责任链模式.
//Abstract Base Type
public abstract class CustomerServiceDesk
{
protected CustomerServiceDesk _nextHandler;
public abstract void ServeCustomers(Customer _customer);
public void SetupHadler(CustomerServiceDesk _nextHandler)
{
this._nextHandler = _nextHandler;
}
}
public class FrontLineServiceDesk:CustomerServiceDesk
{
public override void ServeCustomers(Customer _customer)
{
if (_customer.ComplaintType == ComplaintType.General)
{
Console.WriteLine(_customer.Name + " Complaints are registered ;
will be served soon by FrontLine Help Desk..");
}
else
{
Console.WriteLine(_customer.Name + "
is redirected to Critical Help Desk");
_nextHandler.ServeCustomers(_customer);
}
}
}
public class CriticalIssueServiceDesk:CustomerServiceDesk
{
public override void …Run Code Online (Sandbox Code Playgroud) 我对表达式树的理解是:
表达式树是表达式的内存表示,如算术或布尔表达式.表达式存储在已解析的树中.因此我们可以轻松地转换为任何其他语言.
Linq to SQL使用表达式树.通常在LINQ to SQL查询中,编译器将其转换为已解析的表达式树.它们作为T-SQL语句传递给Sql Server.Sql server执行T-SQL查询并将结果发送回去.这就是执行时的原因LINQ to SQL你IQueryable<T>得不到IEnumetrable<T>.因为IQuerybale包含
public IQueryable:IEnumerable
{
Type Element {get;}
Expression Expression {get;}
IQueryaleProvider Provider {get;}
}
Run Code Online (Sandbox Code Playgroud)
问题:
Microsoft使用Expression树来玩LINQ-to-Sql.我可以使用表达式树来增强代码的不同方法.
除了LINQ to SQL,Linq到amazon,谁在他们的应用程序中使用了表达式树?
Linq to Object返回IEnumerable,Linq to SQL返回IQueryable,LINQ to XML返回什么?
我打电话的时候object.Dispose(); CLR会立即从内存中销毁对象,还是在下一个循环中标记要删除的对象?
我们GC.SuppressFinalize()在Dispose()之后立即调用,这是否意味着,"不要再次收集对象以进行处置,因为它已经提交给了移位".
实际上哪一代负责破坏,我猜第2代.
我从CSV文件收到日期和时间
The received Date format is YYYYMMDD (string) (there is no ":" ,"-","/" to
separate Year month and date).
The received time format is HH:MM (24 Hour clock).
Run Code Online (Sandbox Code Playgroud)
我必须验证两者,以便(例)(i)000011990可以在日期无效(ii)77:90可能会因时间而失效.
问题是 ,
正则表达式是这样做的正确候选者(或)有没有其他方法来实现它?
我在App.Config文件中定义了两个端点作为
<system.serviceModel>
<services>
<service
name="HostDirectAddress.ITestService"
behaviorConfiguration="behaviorConfig">
<endpoint
address="net.tcp://localhost:9000/ITestService"
binding="netTcpBinding"
contract="HostDirectAddress.ITestServiceContract"/>
<endpoint
address="http://localhost:9000/mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="behaviorConfig">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)
我的客户来电
static void Main(string[] args)
{
ServiceHost host =
new ServiceHost(typeof(HostDirectAddress.ITestService));
host.Open();
Console.WriteLine("....Service is Ready to Consume...");
Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
我尝试启动主机时收到以下错误
ServiceMetadataBehavior的HttpGetEnabled属性设置为true,HttpGetUrl属性是相对地址,但没有http基址.提供http基址或将HttpGetUrl设置为绝对地址.
怎么解决?
我被要求解释匿名方法的丑陋和优点.
我可能会解释
丑陋的事情
anonymous methods turning quickly into spaghetti code.
Run Code Online (Sandbox Code Playgroud)
好处
我们可以使用匿名方法生成线程安全代码:示例
static List<string> Names = new List<string>(
new string[] {
"Jon Skeet",
"Marc Gravell",
"David",
"Bill Gates"
});
static List<string> FindNamesStartingWith(string startingText)
{
return Names.FindAll(
delegate(string name)
{
return name.StartsWith(startingText);
});
}
Run Code Online (Sandbox Code Playgroud)
但实际上我不知道它是否是线程安全的.我被要求证明它是正确的.任何人都可以帮助我理解 (1)匿名方法的优点(2)上面的代码线程是否安全?
我理解,通常泛型是编译时安全的,并允许我们保持强类型集合.那么泛型如何允许我们存储匿名类型,如
List<object> TestList = new List<object>();
TestList.Add(new { id = 7, Name = "JonSkeet" });
TestList.Add(new { id = 11, Name = "Marc Gravell" });
TestList.Add(new { id = 31, Name = "Jason" });
Run Code Online (Sandbox Code Playgroud) 是否DataContract在ASP.NET 4.0 WCF中弃用了属性?我只能看到DataContractFormat属性.
我不能在struct上应用DataContractFormat属性.
例
[DataContractFormat]
public struct Contact
{
public string firstName;
public string lastName;
}
Run Code Online (Sandbox Code Playgroud)
它抛出一个错误,说DataContractFormat artribute只能用于类,接口和方法.
如何在Nunit中编写测试来测试我的单例类只创建了一个实例.
我正在处理一些我在My ASP.NET网站的Bin文件夹中复制的CSV文件.
当我执行
using (IDataReader csv = new CsvReader
(new StreamReader("sample.txt"), true, '|'))
{
.....
}
Run Code Online (Sandbox Code Playgroud)
它抱怨我"sample.txt"没有找到" c:\Program Files\.....\"
不会runtime自动查看bin文件夹吗?我需要做什么修改?
c# ×9
asp.net ×5
closures ×2
wcf ×2
assemblies ×1
clr ×1
generics ×1
hosting ×1
linq ×1
singleton ×1
unit-testing ×1
validation ×1