小编Jes*_*cer的帖子

有没有办法用NHibernate返回插入记录的主键?

好吧,这个问题有点惹恼了.我现在正在做这样的事情:

using (var session = _sessionFactory.OpenSession())
{
     using (var transaction = session.BeginTransaction())
     {
         Car newCar = new Car();
         newCar.name = "Jeep";

         session.Save(newCar);
         transaction.Commit();    
     }
}
Run Code Online (Sandbox Code Playgroud)

c# nhibernate

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

在这个.NET代码中遇到Generics的问题

i'm trying to make a mixed collection of Types. I know the types at the start.. but I can't seem to figure out the syntax to make the collection, etc.

eg.

....
// I leave the typo there, for embarrassment :(
Initialize(new []{ typeof(Cat), typeof(Dog), typeof(JohnSkeet) }); 
...

public Foo Initialize(IEnumerable<Type> types)
{
   // for each type, set up the inmemory storage.
   foreach(var type in types)
   {
       // ????
       // Create an empty list, which will only contain this 'type' …
Run Code Online (Sandbox Code Playgroud)

.net c# generics

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

存在System.ArgumentException的Catch块,但无论如何都不会捕获异常,为什么?

我有这个代码:

       if (typeof(Enum).IsAssignableFrom(typeof(T)))
        {
            try
            {
                return (T)Enum.Parse(typeof(T), text);
            }
            catch (ArgumentException e)
            {
                return default(T);
            }
        }
Run Code Online (Sandbox Code Playgroud)

但是我收到了以下例外:

mscorlib.dll中出现'System.ArgumentException'类型的第一次机会异常附加信息:未找到请求值'ABC'

怎么可能?为什么catch块不工作?

c#

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

C#变量名称corpID与corpId

我正在尝试确定C#中的局部变量是否应该命名为"corpID"或"corpId".

看到不同?"d"应该是大写还是小写?

考虑一下这个:

customerPK与customerPk

如果添加另一个单词,那就更重要了.像这样:

customerPKField与customerPkField

看我的delima?是否有一种广泛采用的方法来处理这个问题?

c# naming-conventions

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

同步问题

我的代码中有以下场景(C#.NET 4):

我有一个初始化为false的布尔变量:

bool b = false;
Run Code Online (Sandbox Code Playgroud)

现在我正在实例化线程数,每个线程都需要读取b的值.第一个读取b的线程 - 应该将其值更改为true(并执行一些逻辑...),其他线程应该什么都不做.我是否需要在读取其值时使用"b"同步机制,或者只能在将其值设置为true时锁定它?这应该会提升我的表现,但我想知道它的安全性.

.net c# multithreading synchronization

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

投掷骰子的方法,Yatzee游戏

我在C#中创建了一个简单的游戏(Yatzee).我创建了一个包含5个不同骰子的数组 int[] dice = new int[5];

现在,我想创建一个抛出这五个骰子之一的方法.哪个应该抛出的模具应该作为该方法中的参数传递.这就是我尝试的方式:

public void throwDice(int x)
{
    Random r1 = new Random(6);
    r1.x;
}
Run Code Online (Sandbox Code Playgroud)

我认为正在发生的是,该方法接受一个参数x,随机应该将骰子扔到1-6之间的数字.但我发现了错误,当我写说有:r1.x;

那么,为什么我在这里问,是否能得到一些指导.我在这里走在正确的轨道上,还是我完全迷失了?

c#

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

"方法必须具有返回类型"错误

 public StudentClass(char level, int number, Teacher educator, int maxStudents)
    {
        this.level = level;
        this.number = number;
        this.educator = educator;
        this.numStudents = 0;
        this.students = new Student[maxStudents];

    }
    public StudentClass(char level, int number)
    {
        this.level = level;
        this.number = number;
        this.numStudent = 0;
        this.students = new Student[STANDART];
    }
Run Code Online (Sandbox Code Playgroud)

它给我一个错误的单词 - StudentClass请帮助我....谢谢你

c#

-4
推荐指数
1
解决办法
1932
查看次数