我怀疑答案是否定的,但我想知道是否有可能做到这样的事情:
public class MyGenericClass<TSomeClass> {
public void MyGenericMethod<TSomeInterface>()
// This doesn't compile.
where TSomeClass : TSomeInterface
{
//...
}
}
Run Code Online (Sandbox Code Playgroud)
我在上面(非工作)示例中表示的意思是约束TSomeInterface,使得它可以是任何基类,实现的接口,或者(如果你真的想要得到幻想)隐式转换MyGenericClass.
注意:
我怀疑这个从未在C#中实现的原因是泛型约束并不真正意味着代码契约,这就是我在这里尝试使用它们的方式.我真的不在乎什么类型TSomeInterface,只要它是由实现TSomeClass.
到目前为止,我已经将它们整合在一起:
public class MyGenericClass<TSomeClass> {
public void MyGenericMethod<TIntermediateType, TSomeInterface>()
where TIntermediateType : TSomeClass, TSomeInterface
{
//...
}
}
Run Code Online (Sandbox Code Playgroud)
这或多或少强制执行我想要的约束(TSomeClass必须继承,或者在接口的情况下,实现TSomeInterface),但调用它是非常笨拙的,因为我必须指定TIntermediateType(即使我真的希望它来评估TSomeClass):
var myGenericInstance = new MyGenericClass<TSomeClass>();
myGenericInstance.MyGenericMethod(TSomeClass, TSomeInterface);
Run Code Online (Sandbox Code Playgroud)
另外,上面的hack被破坏了,因为调用者理论上可以指定一个子类TSomeClass作为第一个类型参数,其中只有子类实现TSomeInterface.
我想这样做的原因是,我写了一个WCF服务一口流利的工厂模式,我会想,以防止来电(在编译时)从试图创建与该服务类没有按"合同端点实施.我显然可以在运行时检查这个(WCF实际上是为我做的),但我是编译时检查的忠实粉丝.
是否有更好/更优雅的方式来实现我在这里的目标?
我想知道是否可以添加一个可以在多个map()调用中使用的成员对象.例如,StringBuilder:
private StringBuilder builder;
public void map(...){
...
builder.setLength(0);
builder.append(a);
builder.append(b);
builder.append(c);
d = builder.toString();
...
}
Run Code Online (Sandbox Code Playgroud)
显然,如果映射器对象跨多个线程共享,则由于来自多个线程的并发访问,上面的构建器对象将不会按预期运行.
所以我的问题是:是否确保hadoop中的每个线程都将使用一个专用的映射器对象?或者它是可配置的行为?
谢谢
在 Rust 1.36 中,编译如下:
let arr = [0 as u8; 30];
let buf: Box<[u8]> = Box::new(arr);
Run Code Online (Sandbox Code Playgroud)
但这失败了,并出现错误:expected slice, found array of 30 elements引用带下划线的代码:
let arr = [0 as u8; 30];
let buf: RefCell<[u8]> = RefCell::new(arr);
^^^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)
谁能解释为什么行为不同?和RefCell都Box将 T 约束为<T: ?Sized>。
我正在使用C#4.0开发一个项目.我有几个presenter类,它们都继承自同一个基类.这些演示者类中的每一个都有一个"当前"对象,该对象特定于每个演示者,但它们都有一个共同的继承类,当然与演示者分开.
在可怕的伪代码中:
class ApplicationPresenter inherits BasePresenter
Pubilc PersonApplication Current (inherited from Person)
class RecordPresenter inherits BasePresenter
Public PersonRecord Current (inherited from Person)
class InquiryPresenter inherits BasePresenter
Public PersonInquiry Current (inherited from Person)
Run Code Online (Sandbox Code Playgroud)
...等等
有没有办法让它可以从任何这些中调用"当前",而不需要类型检测,但是它是否与最佳实践保持一致?
我认为最好的选择就是让它成为一个动态的,因为我知道无论我传递给它的是什么,都会有"当前"并且这样做.那是对的吗?
或者有一种方法可以创建:
class BasePresenter
Public Person Current
Run Code Online (Sandbox Code Playgroud)
并适当地进行演员表演?
我知道有很多方法可以解决这个问题,但我正在寻找干净和适当的方法.
谢谢!
在Scala shell中我做了这个:
import java.util._
import scala.collection.JavaConversions._
val t: SortedMap[String,Int] = new TreeMap[String,Int] () // produces an empty java.util.SortedMap
t("a") = 1; t("b") = 2; t("A") = 3; t("0") = 4
t // prints: res35: java.util.SortedMap[String,Int] = {0=4, A=3, a=1, b=2}, as expected
t foreach println // also prints the (k,v) pairs in the same TreeMap sorted order
Run Code Online (Sandbox Code Playgroud)
但是,以下语句不按排序顺序打印对,它似乎以哈希桶顺序打印它们,(0,4)(b,2)(A,3)(a,1):
for ((k,v) <- t) printf("(%s,%d)%n", k, v)
Run Code Online (Sandbox Code Playgroud)
在与for和foreach相关的其他答案中,似乎理解应该转化为使用foreach,如下所示:
"对于(p < - e)e0的理解被翻译为e.foreach {case p => e0}"
但这似乎不是这里发生的事情.
请注意,如果我从scala TreeMap 创建一个scala …
public bool AddArchitecture(DataSet Architecture)
{
base.SQL = "sp_InsertArchitecture";
base.InitializeCommand();
base.AddParameter("@Architecture_ID",
SqlDbType.UniqueIdentifier, 16,
Architecture.Tables.Add("Architecture").Rows.Add("Architecture_ID"));
base.AddParameter("@Architecture_Name",
SqlDbType.VarChar, 25,
Architecture.Tables.Add("Architecture").Rows.Add("Architecture_Name"));
base.AddParameter("@Architecture_Description",
SqlDbType.VarChar, 25,
Architecture.Tables.Add("Architecture").Rows.Add("Architecture_Description"));
base.AddParameter("@Architecture_Date_Added",
SqlDbType.DateTime, 8,
Architecture.Tables.Add("Architecture").Rows.Add("Architecture_Date_Added"));
**AddArchitecture** = ExecuteStoredProcedure; This is where I get the error.
}
Run Code Online (Sandbox Code Playgroud) 我正在使用VS 2010创建一个Windows窗体应用程序.我已经ProgressBar在我的窗体中添加了一个,并且我更改了它的值.但是,ProgressBar不会提前或改变.我认为其中一个原因是因为这个过程太快了,但我做得很慢而且也没用.
这是代码:
void pn_TenPercentProgressEvent(object sender, PrimeNumberEventArgs e)
{
probarPrimeNumber.Visible = true;
probarPrimeNumber.Value = (int)e.Percent * 100;
this.Refresh();
}
Run Code Online (Sandbox Code Playgroud)
这是事件发生的类:
public ulong[] Generate(int count)
{
ulong[] ulngaryResult = new ulong[count];
switch (count)
{
case 0:
{
throw new Exception("The Zero is NOT Permitted.");
}
case 1:
{
ulngaryResult[0] = 2;
break;
}
case 2:
{
ulngaryResult[1] = 3;
goto case 1;
}
default:
{
int intIndex = 0;
double dblPercent = 0.1;
for (ulong i = …Run Code Online (Sandbox Code Playgroud) 这是我的代码.我无法理解是什么问题,但Visual Studio 2008说
运算符(*)不能应用于'object'和'double'类型的操作数
谢谢 ...
#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
#endregion
namespace DailyRate
{
class Program
{
static void Main(string[] args)
{
(new Program()).run();
}
public void run()
{
double dailyRate = readDouble("Enter your daily rate: ");
int noOfDays = readInt("Enter the number of days: ");
writeFree(CalculateFee(dailyRate, noOfDays));
}
private void writeFree(object p)
{
Console.WriteLine("The consultant's fee is: {0}", p * 1.1);
}
private double CalculateFee(double dailyRate, int noOfDays)
{
return dailyRate * noOfDays;
} …Run Code Online (Sandbox Code Playgroud)