小编Sri*_*vel的帖子

TimeSpan.Parse错误

TimeSpan timeTaken = TimeSpan.Parse("51:45:33");
Run Code Online (Sandbox Code Playgroud)

给我错误:

mscorlib.dll中出现"System.OverflowException"类型的异常,但未在用户代码中处理

附加信息:无法解析TimeSpan,因为至少有一个数字组件超出范围或包含太多数字.

为什么?

c# parsing timespan

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

图像的压缩类型

private static int GetCompressionType(Image image)
{
    // The zero-based index of the first occurrence within the entire array, if found; otherwise, –1.

    int compressionTagIndex = Array.IndexOf(image.PropertyIdList, 0x103);
    PropertyItem compressionTag = image.PropertyItems[compressionTagIndex];
    return BitConverter.ToInt16(compressionTag.Value, 0);
}
Run Code Online (Sandbox Code Playgroud)

程序员你好。我写了一个程序,我可以根据用户输入旋转图像(任何类型的图像)。担心的是当我旋转 tiff 图像时,它实际上是根据用户输入旋转的,但我松开了压缩,所以使用了上面的方法(用谷歌搜索)..现在我对此有一些疑问..

  1. 为什么是特定的0x103???
  2. 此方法是否特定于 Tiff ?
  3. 对于 JPG、JPEG 或 BMP,我是否需要查看压缩类型?

c#

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

如何处理多个异步方法?

我怎么知道异步(等待)操作是否已经在运行并等待应用程序中的完成.我在视图中有两个按钮,每个按钮绑定到两个不同的异步方法.如果单击button1并启动异步并等待其结果.并且在那个时候点击button2.我需要显示一条消息,即已经运行的异步方法存在,并停止执行第二个异步方法.我怎样才能实现这一目标?

c# asynchronous winrt-xaml winrt-async windows-store-apps

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

Box C#对象作为通用接口

我有大量的视图模型,每个视图模型都是针对一种特定的对象类型设计的(例如Person,Car,等等).我想为这些添加通用功能,例如检查是否已存在包含特定对象(包含特定Car,Person等)的viewmodel的功能.我希望能够动态创建ViewModels(例如,为Car创建viewmodel,为Person创建viewmodel).所有视图模型都将具有不同的功能,命令等,因此我不能简单地使用一个全局视图模型.是否可以使用接口来解决这个问题,或者这是一个根本上有缺陷的设计模式?

当前代码:

namespace DummyNameSpace
{

    public interface IViewModel<T>
    {
        T DataModelObject { get; set; }
        string Name { get; set; }
    }

    public class SomeDataModelObject
    {
        public string SomeProperty { get; set; }
    }

    public class ViewModelInstance : IViewModel<SomeDataModelObject> //There will be various ViewModels that implement IViewModel, all with different 'T' types.
    {
        //Some properties, commands, etc.
    }

    public class TheMainViewModel //won't implement IViewModel, as this will be used to control all the various other viewmodels.
    {

        public void MethodBeingCalled(object …
Run Code Online (Sandbox Code Playgroud)

c# generics interface

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

使用具有两种类型但具有不同名称的API.我如何重用我的代码?

我正在使用一个Web服务,它返回两种类型完全相同但被称为两种不同的东西(是的,我知道的API很棒......).一种叫做SearchKMQueryResponse,另一种叫做TopSolutionsKMQueryResponse.我将这些类型的输出映射到我自己的模型类中,并且必须使用两个采用不同参数类型的方法,但返回相同的类型.这两种API类型继承了该object类型,因此我无法将基类型作为参数传递.它们不通过API公开接口,所以我也搞砸了.

那么有一个优雅的解决方案,以停止重复自己在下面的代码?...

编辑:我正在使用.Net v3.5来解决这个问题.

      public static KmSearchResponse Map(SearchKMQueryResponse response)
    {
        if (response == null)
        {
            return null;
        }
        else
        {
            var myResponse = new KmSearchResponse()
            {
                CountTotal = long.Parse(response.model.instance.resultlist.resultlist[0].Value),
                Message = response.message,
                Status = (MyProject.Model.StatusType)response.status,
            };

            for (var startID = 2; startID < (myResponse.CountTotal * 5); startID += 5)
            {
                myResponse.Results.Add(
                    KmSearchResponseResultsMapper.Map(
                    response.model.instance.resultlist.resultlist, startID)
                    );
            }

            myResponse.Count = myResponse.Results.Count;

            return myResponse;
        }
    }

    public static KmSearchResponse Map(TopSolutionsKMQueryResponse response)
    {
        if (response == null)
        { …
Run Code Online (Sandbox Code Playgroud)

.net c# generics types .net-3.5

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

使用语句最佳实践

这可能是一个愚蠢的问题,但由于我不能自己给出答案,我会在这里提出.

让我们在http处理程序中使用一个模块(Web应用程序是使用ASP.NET用C#编写的),并让这个模块实现IDisposable接口.然后一个常见的方法是使用如下模块:

using(var module = new ModuleName(param1, param2, param3))
{

}
Run Code Online (Sandbox Code Playgroud)

是否更好地将任何代码放在我们将仅在此using语句的主体内部或之前使用的变量中.在代码方面:

是第一种方法还是第二种方法更好(以及为什么):

第一种方法

using(var module = new ModuleName(param1, param2, param3))
{
    int a = Request.GetParam<int>("aNumber");
    string b = Request.GetParam<string>("bString");
    // and other parameters contained to the http request
}
Run Code Online (Sandbox Code Playgroud)

第二种方法

int a = Request.GetParam<int>("aNumber");
string b = Request.GetParam<string>("bString");
// and other parameters contained to the http request

using(var module = new ModuleName(param1, param2, param3))
{

}
Run Code Online (Sandbox Code Playgroud)

如果没有任何技术原因 - 这是基于意见的决定 - 我们应该更喜欢第二种方法的第一种方法,反之亦然,请告诉我,以便删除我的帖子.

c# using

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

如果接口是在业务层而不是数据访问层中定义的,则您不了解接口

请有人帮我澄清一下我依赖倒置原则的问题.如果我在DAL中有一个看起来像这样的存储库,并且在DAL中有相应的接口.我基本上是对那些会使用我的DAL的人说,"这是一个名为'FindEvents'的接口,这是我通过接口签订的合同.所以开发人员知道不要直接使用该对象,而是使用我的界面.我可以甚至将对象设为私有,只将接口公开为public

达尔 -

public class YogaSpaceEventRepository : IYogaSpaceEventRepository
{
    public IQueryable<YogaSpaceEvent> FindEvents(DateTime start, DateTime end)
    {
    // Retrieve Data from Database
    }
} 

public interface IYogaSpaceEventRepository : IDisposable
{
    // here my repo (data access layer) is referencing my business layer to return YogaSpaceEvent
    IQueryable<YogaSpaceEvent> FindEvents(DateTime start, DateTime end); 
}
Run Code Online (Sandbox Code Playgroud)

但是如果我取出这个界面并将其粘贴在BLL(DDD方法)中,那么我可以避免循环引用(DAL需要引用BLL来理解它正在返回的对象'YogaSpaceEvent'以及我需要调用的BLL中的某个地方FindEvents - 现在它是循环的).这完全打破了界面规则!?因为现在如果DAL开发人员移交DAL程序集,那么作为使用该DAL程序集的开发人员将不知道可以更改的内容(没有合同 - DAL中没有接口)!

通过推杆

public interface IYogaSpaceEventRepository : IDisposable
{
    // here my repo (data access layer) is referencing my business layer to return YogaSpaceEvent
    IQueryable<YogaSpaceEvent> FindEvents(DateTime start, …
Run Code Online (Sandbox Code Playgroud)

c# domain-driven-design dependency-injection n-tier-architecture

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

为什么Task <TResult>在等待之前等待结果?

我想了解TPL.不幸的是,我无法通过返回类型来完成任务.从我读过的内容来看,我认为将任务分配给变量会异步启动它.当您需要返回值时,您只需等待它,这可确保当前线程等待Task<T>完成.

MSDN上的示例:

// Call and await in separate statements.
Task<int> integerTask = TaskOfT_MethodAsync();

// You can do other work that does not rely on integerTask before awaiting.
textBox1.Text += String.Format("Application can continue working while the Task<T> runs. . . . \r\n");

int result2 = await integerTask;
Run Code Online (Sandbox Code Playgroud)

我的理解:第一个语句应该开始执行任务,之后立即附加文本框.然后线程被阻塞直到integerTask完成.

但是,当我自己尝试时,它不会那样工作:

static void Main()
{
    var task = new Task(RunItAsync);
    task.Start();
    task.Wait();
}
static async void RunItAsync()
{
    // Should start the task, but should not block
    var …
Run Code Online (Sandbox Code Playgroud)

c# asynchronous task-parallel-library async-await

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

C#单选按钮数组,如何查找选中

我有以下c#代码将创建一个单选按钮数组.如何在不单独按下每个按钮的情况下检查选择了哪个按钮?

谢谢.

 public Form1()
        {
            InitializeComponent();
            string[] stringArray = new string[3];

            stringArray[0] = "Yes";
            stringArray[1] = "No";
            stringArray[2] = "Maybe";

            System.Windows.Forms.RadioButton[] radioButtons = new System.Windows.Forms.RadioButton[3];

            for (int i = 0; i < 3; ++i)
            {
                radioButtons[i] = new RadioButton();
                radioButtons[i].Text = stringArray[i];
                radioButtons[i].Location = new System.Drawing.Point(10, 10 + i * 20);
                this.Controls.Add(radioButtons[i]);
            }
        }
Run Code Online (Sandbox Code Playgroud)

.net c# winforms

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