问题列表 - 第11940页

当存在接口和实现时,Var关键字类型推断歧义

举个例子:

interface IEntity {
    string Name { get; set; }
}

class Product : IEntity {
    public string Name { get; set; }
    public int Count { get; set; } // added member
}

class Client {
    void Process() {
        var product = new Product();
        int count = product.Count; // this is valid            

    }
}
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,什么是产品类型?是IEntity还是产品?看来产品是具体实施类型(产品).如果是这种情况,则不应仅在特殊情况下使用var.但我发现像resharper这样的工具建议默认使用var.不应该有一个程序到界面?

c#

5
推荐指数
3
解决办法
669
查看次数

Ninject相当于Unity RegisterInstance方法

Ninject是否具有统一注册实例的等效方法.

我想创建一个模拟对象并注册它.

谢谢

unit-testing ninject mocking unity-container

5
推荐指数
1
解决办法
1728
查看次数

这段代码是线程安全的吗?如何使其线程安全?

我有一个带有安全类的WCF服务,用于获取调用用户的一些属性.然而,当涉及到线程安全时,我非常糟糕 - 到目前为止,我并不需要对它做太多的事情,而只是对多线程问题有一个基本的理论上的理解.

鉴于以下功能:

public class SecurityService
{
    public static Guid GetCurrentUserID()
    {
        if (Thread.CurrentPrincipal is MyCustomPrincipal)
        {
            MyCustomIdentity identity = null;
            MyCustomPrincipal principal = (MyCustomPrincipal)Thread.CurrentPrincipal;
            if (principal != null)
            {
                identity = (MyCustomIdentity)principal.Identity;
            }

            if (identity != null)
            {
                return identity.UUID;
            }
        }
        return Guid.Empty;
    }
}
Run Code Online (Sandbox Code Playgroud)

如果从2个不同的线程同时调用该方法,是否有可能出现问题?在我的噩梦中,如果这些方法出错,我会看到可怕的后果,例如有人意外地获取其他人的数据或突然变成系统管理员.一位同事(他也不是专家,但他比我好)认为它可能没问题,因为那里没有真正的共享资源.

或者这个将访问数据库的人 - 这可能会出错吗?

    public static User GetCurrentUser()
    {
        var uuid = GetCurrentUserID();
        if (uuid != null)
        {
            var rUser = new UserRepository();
            return rUser.GetByID(uuid);
        }
        return null;
    }
Run Code Online (Sandbox Code Playgroud)

关于线程的原理有很多讨论,但是当涉及到实际应用它时,我倾向于陷入困境并且知道何时应用它.任何帮助赞赏.

如果不清楚,我可以更详细地解释这些功能的背景/目的.

编辑: …

.net c# wcf multithreading thread-safety

6
推荐指数
1
解决办法
1566
查看次数

如何以编程方式启用/禁用IE代理设置?

我想以编程方式为我的Internet Explorer启用/禁用代理 - C#可以帮助我这方面吗?

提前谢谢,Ravi Naik.

c#

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

为什么要使用strncpy而不是strcpy?

编辑:我添加了示例的源代码.

我遇到了这个例子:

char source[MAX] = "123456789";
char source1[MAX] = "123456789";
char destination[MAX] = "abcdefg";
char destination1[MAX] = "abcdefg";
char *return_string;
int index = 5;

/* This is how strcpy works */
printf("destination is originally = '%s'\n", destination);
return_string = strcpy(destination, source);
printf("after strcpy, dest becomes '%s'\n\n", destination);

/* This is how strncpy works */
printf( "destination1 is originally = '%s'\n", destination1 );
return_string = strncpy( destination1, source1, index );
printf( "After strncpy, destination1 becomes '%s'\n", destination1 );
Run Code Online (Sandbox Code Playgroud)

哪个产生了这个输出: …

c buffer-overflow c89 strcpy strncpy

77
推荐指数
5
解决办法
17万
查看次数

如何访问子窗口的dom树?

我使用以下代码打开一个新窗口:

purchaseWin = window.open("Purchase.aspx","purchaseWin2", "location=0,status=0,scrollbars=0,width=700,height=400");
Run Code Online (Sandbox Code Playgroud)

我想访问purchaseWin的dom树,例如

purchaseWin.document.getElementById("tdProduct").innerHTML = "2";
Run Code Online (Sandbox Code Playgroud)

它不起作用.我只能这样做:

purchaseWin.document.write("abc");
Run Code Online (Sandbox Code Playgroud)

我也试过这个,它也不起作用:

 $(purchaseWin.document).ready(function(){

     purchaseWin.$("#tdProduct").html("2");

   });
Run Code Online (Sandbox Code Playgroud)

我该怎么办?

javascript

18
推荐指数
3
解决办法
3万
查看次数

如何在while循环期间获取用户输入而不阻塞

我正在尝试编写一个while循环,通过使用os.system("clear")不断更新屏幕,然后每隔几秒打印一个不同的文本消息.如何在循环期间获得用户输入?raw_input()只是暂停和等待,这不是我想要的功能.

import os
import time

string = "the fox jumped over the lazy dog"
words = string.split(" ")
i = 0 

while 1:
    os.system("clear")
    print words[i]
    time.sleep(1)
    i += 1
    i = i%len(words)
Run Code Online (Sandbox Code Playgroud)

我希望能够在中间按'q'或'p'分别退出和暂停.

python

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

CodeIgniter:开发和生产环境

我目前正在学习CodeIgniter PHP框架.目前,我正在寻找DEV环境和PRODUCTION环境.来自纯粹的C和JAVA背景,我习惯在本地拥有所有东西(带版本控制),但由于我将在网站上有PRODUCTION方面,我想知道几件不同的事情:

  1. 将DEV环境置于本地更好吗?
  2. 在DEV端进行更改以使其进入PRODUCTION端时(假设DEV env是本地的),这将是一种好的(和简单的)方式?
  3. 是否有可能(如果是这样,如何?)设置CodeIgniter以在同一代码空间中具有DEV&PROD环境(比如在服务器上但具有不同的数据库表)?
  4. 在我将在Framework中创建的应用程序上使用Version Control时,是否建议只为我的应用程序创建文件,或者添加整个Framework(以保持代码与Framework的版本一致)?

我很感激您提前提出的任何想法或建议.

谢谢!

php version-control frameworks codeigniter

6
推荐指数
1
解决办法
4300
查看次数

提升使用mach_inject的权限

如何使用Authorization API将用户权限提升为root,以便可以使用mach_inject?

macos code-injection

6
推荐指数
1
解决办法
2058
查看次数

为什么小部件的创建顺序很重要?

以下代码工作正常.它显示一个panedwindow,顶部有一个蓝色框,下面是一个绿色框:

panedwindow .root -orient vertical -showhandle true -background red
frame .top -background blue -width 100 -height 100
frame .bot -background green -width 100 -height 100
.root add .top .bot
pack .root -expand true -fill both
Run Code Online (Sandbox Code Playgroud)

但是,当我panedwindow向下移动命令时,事情就会停止工作.顶部的蓝色框未显示.相反,panedwindow它本身的红色闪耀着:

frame .top -background blue -width 100 -height 100
panedwindow .root -orient vertical -showhandle true -background red
frame .bot -background green -width 100 -height 100
.root add .top .bot
pack .root -expand true -fill both
Run Code Online (Sandbox Code Playgroud)

为什么会这样?可以将panedwindow真的只管理后创建的小部件?我已经看到了与打包器类似的行为,它将拒绝打包-in …

tk-toolkit tcl

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