小编Joh*_*ers的帖子

编写实现接口所需的泛型类型的方法

假设有一个名为A的类,其方法如下:

public void SomeMethod<T>(ref T para1)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

现在我有一个接口(让我们称之为ITest)强制执行一个方法(让我们称之为RequiredMethod())并且有一堆实现此接口的类.

在SomeMethod的声明中,我怎么能提到T需要实现ITest以便我可以做这样的事情?

public void SomeMethod<T implements ITest>(ref T para1)
{
    ...
    para1.RequiredMethod();
    ...
}
Run Code Online (Sandbox Code Playgroud)

c# generics interface

0
推荐指数
1
解决办法
86
查看次数

.NET ProcessorAffinity计算

我找不到任何有关如何正确计算 c# 的 ProcessorAffinity 上的亲和力设置的文档。

我正在传递选定核心的列表并添加它们以获得最终的亲和力设置,但是我的计算似乎已关闭。

如何根据所选核心 (0-32/64) 的列表正确计算亲和力。

另外这该如何优化呢?

if (!string.IsNullOrEmpty(affinitystr) && !affinitystr.Equals("-1"))
{
    string[] words = affinitystr.Split(',');
    int affinitytotal = 0;
    foreach (string word in words)
    {
        int affinity = Convert.ToInt32(word);
        if (affinity == 0) { affinity = 1; } // core 0
        else if (affinity == 1) { affinity = 2; } // core 1 
        else if (affinity == 2) { affinity = 4; } // core 2
        else if (affinity == 3) { affinity = 8; …
Run Code Online (Sandbox Code Playgroud)

c#

0
推荐指数
1
解决办法
304
查看次数

父级的<list>,为每个子类做

所以,我有一个类GameObject,我用它作为许多子类的基础.

class GameObject()
{
    blah;
}

class AmmoBox() : Gameobject()
{
    different blah;
}

class Taco() : GameObject() {very different blah;}
Run Code Online (Sandbox Code Playgroud)

后来我在列表中使用它们,我想为每个子类指定不同的更新/ etc行为.

foreach (AmmoBox a in GameObjects) { }

foreach (Taco t in GameObjects) { }
Run Code Online (Sandbox Code Playgroud)

编译器抛出错误"无法将'GameObject'类型的对象强制转换为'AmmoBox'.我知道这是一个可怜的错误,我该怎么做呢?

c# vb.net xna list subclass

0
推荐指数
1
解决办法
334
查看次数

.length无法解析或不是字段

我已经尝试过很难用之前的类似答案来解决这个问题,但我仍然无法看到我的问题,希望你能提供帮助.我的代码看起来像这样:

String MyContent =" ";
String nextline = " ";

InputStream in = new FileInputStream(f);

BufferedInputStream bin = new BufferedInputStream(in);

DataInputStream din = new DataInputStream(bin);

    while(din.available()>1)
    {
    nextline = din.readLine();

    //Filter out XML headers which are not browser compliant
    if (nextline.length > 4)
        {
        if (nextline.substring(1,5) != "<?xml")
            {
            MyContent=MyContent+ nextline;
            }
        }   
    }

    out.print (MyContent);

in.close();
bin.close();
din.close();
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

An error occurred at line: 25 in the jsp file: /MaxiSunReports/DisplayXMLFile.jsp
nextline.length cannot be resolved or is not a field …
Run Code Online (Sandbox Code Playgroud)

java string

0
推荐指数
1
解决办法
1万
查看次数

单元测试C#数据库相关代码

我想为数据相关代码创建单元测试.例如:

具有通常的创建,更新和删除的用户类.

如果我想为"用户已存在"方案创建测试,或者更新或删除测试.我需要知道我的数据库中已存在特定用户.

在这种情况下,对于可以按任何顺序运行的这些操作进行独立测试的最佳方法是什么?

c# database dependencies unit-testing

0
推荐指数
1
解决办法
1045
查看次数

属性设置器上的堆栈溢出

我在C#中有一个具有当前属性的对象.

public DateTime startDate
{
    get 
    {
        string[] ymd = Environment.GetCommandLineArgs()[2].Split('.');
        return new DateTime(Int32.Parse(ymd[2]), Int32.Parse(ymd[1]), Int32.Parse(ymd[0])); 
    }
    set { startDate = value; }
}
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用定义为此的函数时:

public String Calculate(){
    if (startDate > endDate)
        return "not calculable since the end date can not be before than the start date.";

    while (startDate <= endDate)
    {
        if (startDate.DayOfWeek.ToString()[0] != 'S')
            count++;
        startDate = startDate.AddDays(1);
    }

    return "not implemented yet";
Run Code Online (Sandbox Code Playgroud)

Stack Overflow发生:)你可以帮我解决这个问题吗?

c# setter properties

0
推荐指数
1
解决办法
117
查看次数

由于某种原因,不能隐式地将类型'string'转换为'int'

此代码将电子邮件发送到访问数据库中保存的多个电子邮件地址,但我在line(email =read_Email.GetValue(i).ToString();)中遇到问题不能隐式地将类型'string'转换为'int'

任何帮助.

try
{
    ArrayList list_emails = new ArrayList();
    int i = 0, email = 0;
    connection.Open(); //connection to the database.
    OleDbCommand cmd_Email = new OleDbCommand("Select Email from Email_Table", connection);
    OleDbDataReader read_Email = cmd_Email.ExecuteReader();
    while (read_Email.Read())
    {
        email =read_Email.GetValue(i).ToString();
        list_emails.Add(email); //Add email to a arraylist
        i = i + 1 - 1; //increment or ++i
    }
    read_Email.Close();
    connection.Close(); //Close connection

    foreach (string email_to in list_emails)
    {
        MailMessage mail = new MailMessage();
        mail.To.Add(email_to);
        mail.Subject = label2.Text + " …
Run Code Online (Sandbox Code Playgroud)

c# email ms-access

0
推荐指数
1
解决办法
269
查看次数

在本地环境之外使用时,Google Calendar V3会挂起

我正在研究Google Calendar API的.net版本的包装器.身份验证非常简单,在本地工作正常(localhost:port).

UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
               new ClientSecrets
               {
                   ClientId = "The id of my public website",
                   ClientSecret = "The secret of my public website",
               },
               new[] { CalendarService.Scope.Calendar },
               "user",
               CancellationToken.None).Result;

            // Create the service.
            var service = new CalendarService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential
            });

return service.Events.List("primary").Execute().Items;
Run Code Online (Sandbox Code Playgroud)

但问题是,当相同的代码部署到生产环境时(当然,密码已更改),只要我尝试访问数据,应用程序就会挂起.没有例外,没有错误代码,它只是永远加载.

有没有人经历过这个?

c# asp.net oauth google-calendar-api google-api

0
推荐指数
1
解决办法
1668
查看次数

Primefaces lazydatamodel加载覆盖不起作用

我有覆盖primefaces lazydatamodel加载的问题.加载方法的错误点.我正在使用primefaces 5.0.在Jboss Developer Studio 7.1中工作

 private LazyDataModel<City> mdlCityList;
    @PostConstruct
        public void init() {
            try {
                this.mdlCityList = new LazyDataModel<City>() {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public List<City> load(int first, int pageSize,
                            String sortField, SortOrder sortOrder,
                            Map<String, String> filters) {
                        mdlCityList.setRowCount(cityFacade.count(filters));
                        return cityFacade.getResultList(first, pageSize, sortField,
                                sortOrder, filters);
                    }
                };
                mdlCityList.setRowCount(cityFacade
                        .count(new HashMap<String, String>()));
            } catch (Exception e) {
                System.out.println("Exception in CityListProducer " + e);
            }
        }
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我正在实现@Override,但它仍然指出:

new LazyDataModel(){}类型的方法load(int,int,String,SortOrder,Map)必须覆盖或实现超类型方法

jsf primefaces jsf-2 lazydatamodel

0
推荐指数
1
解决办法
6467
查看次数

HttpClient 在 GetStringAsync 方法上冻结

我有这个 c# MVC 代码,它与 GetAsync 和 GetPost 方法一起工作得很好,但是当使用 GetStringAsync 时,它在该行冻结:

version = await client.GetStringAsync("/API/Version");
Run Code Online (Sandbox Code Playgroud)

驱动程序代码:

Task<string>[] tasks = new Task<string>[count];
for (int i = 0; i < count; i++)
{
    tasks[i] = MyHttpClient.GetVersion(port, method);
}
Task.WaitAll(tasks);
string[] results = new string[tasks.Length];
for(int i=0; i<tasks.Length; i++)
{
    Task<string> t = (Task<string>)(tasks[i]);
    results[i] = (string)t.Result;
}
Run Code Online (Sandbox Code Playgroud)

HttpCilent 代码:

public static async Task<string> GetVersion(int port, string method)
{
    try
    {
        var client = new HttpClient();
        client.BaseAddress = new Uri("http://localhost:" + port);
        string version …
Run Code Online (Sandbox Code Playgroud)

c# httpclient

0
推荐指数
1
解决办法
4225
查看次数