小编Rah*_*han的帖子

从C#中的实体框架生成的类派生

我创建了一个实体数据模型并从中生成了一个数据库.

其中一个实体被称为Template.

创建了部分类来扩展工作的功能Template.

如果我创建一个新类并尝试派生Template,我在实例化时得到一个运行时异常:

Mapping and metadata information could not be found for EntityType 'Template001'.

我该如何解决这个问题?我肯定需要继承EF类.

编辑

似乎不可能.如果是这种情况,那么实现以下要求的最佳方式是:模板实体存储有关每个都有自己的代码要执行的模板的信息.这就是为什么我试图从实体派生出来的原因.

c# inheritance entity-framework class deriving

5
推荐指数
2
解决办法
2813
查看次数

在运行时创建一个类

是否可以在运行时创建一个类并使用它?

我不是指在运行时编译代码,生成新程序集,加载它,执行它等等.反射是一个单行道,因为它可以为您提供有关当前加载的程序集的运行时信息或者你从文件或内存加载.

我的问题更多的是关于注射的问题.您可以在自己的应用程序域中的运行时创建一个类,并使它做任何有用的事情吗?

.net c# reflection runtime

5
推荐指数
2
解决办法
4979
查看次数

正 System.Double 值的快速地板和天花板替代方案

double d = 0; // random decimal value with it's integral part within the range of Int32 and always positive.
int floored = (int) Math.Floor(d); // Less than or equal to.
int ceiled  = (int) Math.Ceiling(d); // Greater than or equal to.
int lessThan = ? // Less than.
int moreThan = ? // Greater than.
Run Code Online (Sandbox Code Playgroud)

地板和天花板函数包括分别小于/大于或等于 的最大/最小整数d。我想找出分别小于/大于但不等于 的最大/最小整数d

当然,这可以通过一些if's and but's方法来实现,但我正在寻找一种不包括分支或至少非常快的方法,因为此操作将在算法中执行数十亿次。

二进制操作可能吗?如果没有,最好的选择是什么?

显而易见的解决方案是这样的:

int lessThan = (d - floored) > double.Epsilon …
Run Code Online (Sandbox Code Playgroud)

.net c# double floor ceil

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

将解决方案更改为x64后,编译器指令#if不再识别DEBUG

我正在使用Visual Studio 2010 C#.

除了更改所有项目的构建属性之外,我最近使用配置管理器更改了x64解决方案中的所有项目.从那以后,该#if (DEBUG)指令不起作用,好像DEBUG常数消失了.

#if (DEBUG)
    // This code does not execute even in debug mode!
#endif
Run Code Online (Sandbox Code Playgroud)

我还注意到,而不是通常bin\Debugbin\Release文件夹,现在项目编译成bin\x64\Debugbin\x64\Release.

我搜索了一下,但大多数相关结果都包含如何配置,x64如果它默认情况下不显示.

c# visual-studio-2010 c-preprocessor

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

增加线程和进程优先级以减少处理器密集型并行应用程序的执行时间

我知道设置线程优先级是堆栈溢出的一个禁忌话题,但我确信我的应用程序是提高优先级的好候选人。为了证明这一点,我在下面解释了上下文。现在的问题是如何有效地做到这一点?

该应用程序是 .NET 4 (C#) 控制台应用程序,它执行复杂的算法,执行时间约为 5 小时。该算法根本不是内存密集型的,只是处理器密集型的。它执行数字运算,不执行任何磁盘 I/O、数据库连接、网络连接等。应用程序的输出只是一个最后写入控制台的数字。换句话说,该算法是完全自包含的,没有任何依赖关系。

该应用程序在其自己的专用 16 核 64 位机器上运行,该机器运行 Windows Server,其可用 RAM 远远超过其所需 (8GB)。专用我的意思是已购买服务器以独家运行此应用程序。

我已经通过广泛的分析、花哨的数学快捷方式和一些小技巧对代码进行了尽可能多的优化。

以下是伪代码的整体结构:

public static void Main ()
{
    Process.GetCurrentProcess().PriorityBoostEnabled = true;
    Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime;

    // Of course this only affects the main thread rather than child threads.
    Thread.CurrentThread.Priority = ThreadPriority.Highest;

    BigInteger seed = SomeExtremelyLargeNumber; // Millions of digits.

    // The following loop takes [seed] and processes some numbers.
    result1 = Parallel.For(/* With thread-static variables. */);

    while (true) // Main loop that …
Run Code Online (Sandbox Code Playgroud)

.net c# optimization performance multithreading

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

我在哪里可以找到System.Numerics.BigInteger的源代码?

我需要获得ORIGINAL源代码,System.Numerics.BigInteger以获得开发人员评论和合理的变量名称.我知道如何反编译程序集,这不是我正在寻找的答案.

我尝试过的:

  • 下载.NET 4和4.5的源代码但由于某种原因不包括BigInteger.
  • 源代码通过遵循MS指令步进但由于SP1的这个问题而无法工作,我没有选择在我的开发机器上回滚.我也无法访问另一台具有开发功能的机器.

我相信很多人都使用源步进来调试和/或查看BCL源代码.如果有,请发布BigInteger和相关代码作为答案.如果这样做跨越任何法律界限,请让我知道相同,以便我可以跳过替代方案.如果您确实建议了替代方案,请说明具体原因.

.net c# debugging visual-studio-2010

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

我可以定义具有相同名称但不同参数的2个代理吗?

我试图在Int32和之间定义一个委托覆盖IntPtr.为什么以下超载非法?

public delegate int EnumWindowsCallback (System.IntPtr hWnd, int lParam);

public delegate int EnumWindowsCallback (System.IntPtr hWnd, System.IntPtr lParam);
Run Code Online (Sandbox Code Playgroud)

这看起来很奇怪.它们都是结构但是不同并且从不同的接口实现.

想想看,我从来没有尝试过代理过代表.它甚至是合法的,如果是的话,为什么?

更新:在完成答案和更多SO帖子之后,我感到困惑的是,即使使用不同数量的参数也无法声明代表.我仍然想知道为什么在运行时无法解决这个问题.

.net c# delegates overloading

5
推荐指数
2
解决办法
5152
查看次数

输出重定向时控制台窗口不可见

我正在使用 .NET 4 控制台应用程序中的以下代码:

private static void AttachToConsole ()
{
    System.Diagnostics.Process process = null;

    process = new Process();
    process.StartInfo.FileName = "cmd.exe";
    process.StartInfo.RedirectStandardInput = true;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.CreateNoWindow = false;
    process.EnableRaisingEvents = true;
    process.Start();

    process.OutputDataReceived += new DataReceivedEventHandler(Process_OutputDataReceived);

    Console.Write("Press any key to continue...");
    Console.ReadKey();

    process.OutputDataReceived -= new DataReceivedEventHandler(Process_OutputDataReceived);
    process.CloseMainWindow();
    process.Close();
}
Run Code Online (Sandbox Code Playgroud)

运行时,仅显示应用程序本身的控制台窗口,但[cmd.exe]进程窗口保持不可见。这是为什么?我该如何改变这种行为?

.net c# console console-application

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

实体框架6中的一对多关系

我有两个简单的表必须映射到现有数据库中的列:

public class StockItem
{
    public System.Guid pkStockItemID { get; set; }

    public virtual List<StockItem_ExtendedProperties> ExtendedProperties { get; set; }
}

public class StockItem_ExtendedProperties
{
    public System.Guid pkStockItem_ExtendedPropertiesID { get; set; }

    public System.Guid fkStockItemId { get; set; }

    public virtual StockItem StockItem { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

以下是两者的配置类:

public StockItemConfiguration ()
{
    this.HasMany(item => item.ExtendedProperties)
        .WithRequired(property => property.StockItem)
        .HasForeignKey(property => property.fkStockItemId)
        .WillCascadeOnDelete();
}

public StockItem_ExtendedPropertiesConfiguration ()
{
    this.HasRequired(property => property.StockItem)
        .WithMany(item => item.ExtendedProperties)
        .HasForeignKey(property => property.fkStockItemId)
        .WillCascadeOnDelete();
}
Run Code Online (Sandbox Code Playgroud)

这导致错误: …

.net c# entity-framework

5
推荐指数
2
解决办法
5764
查看次数

Determine if an Enum value is NOT a composite in C#

EDIT:

Most people suggest that flag enums should always have values of powers of two. That may be best practice but I am not defining enums here, rather checking them and want to cover all possible scenarios within reason. The question is really about the proper way to implement the function named EnumUtilities.IsValueDefinedAndComposite<T>.

ORIGINAL POST:

Consider the following Enum:

[Flags]
public enum TestWithFlags { One = 1, Two = 2, }
Run Code Online (Sandbox Code Playgroud)

Following is the result of Enum.IsDefined with various …

.net c# enums enumeration enum-flags

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