小编smn*_*ino的帖子

C#转换继承的通用接口

我遇到了一些麻烦,让我开始构建一个我想出的界面.这是C#Windows Forms的MVP设计.我有一个IView类,我在我的表单类上实现.还有一个IPresenter,我将其导入各种特定的演示者.每个Presenter将根据角色以不同方式管理IView,例如打开对话框以使用AddPresenter输入新数据集,而不是使用EditPresenter编辑现有数据,EditPresenter会将数据预加载到表单上.其中每个都继承自IPresenter.我想使用这样的代码:

AddPresenter<ConcreteView> pres = new AddPresenter<ConcreteView>();
Run Code Online (Sandbox Code Playgroud)

我基本上有这个工作,但这些演示者和他们管理的视图被捆绑到运行时加载的插件,这意味着我需要一个作为插件接口的Manager类采用"模式"参数.此模式参数用于工厂方法以创建"添加"或"编辑演示者",但由于稍后会调用显示对话框,因此我需要通过IPresenter接口进行调用,如下所示:

private IPresenter<IView> pres;
public ShowTheForm()
{
    pres.ShowDialog();
}
Run Code Online (Sandbox Code Playgroud)

现在我遇到的问题是将AddPresenter的具体实例表示为'pres'成员.这是我所拥有的简化版本:

interface IView
{
    void ViewBlah();
}

interface IPresenter<V> where V : IView
{
    void PresBlah();
}

class CView : IView
{
    public void ViewBlah()
    {        
    }
}

class CPresenter<T> : IPresenter<T> where T : IView
{
    public void PresBlah()
    {
    }
}

private void button3_Click(object sender, EventArgs e)
{
    CPresenter<CView> cpres = new CPresenter<CView>();
    IPresenter<IView> ipres = (IPresenter<IView>)cpres;
}
Run Code Online (Sandbox Code Playgroud)

这是错误:

Unable to cast …
Run Code Online (Sandbox Code Playgroud)

c# generics casting interface covariance

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

GZipStream和解压缩

我有应该进行压缩的代码:

FileStream fs = new FileStream("g:\\gj.txt", FileMode.Open);
FileStream fd = new FileStream("g:\\gj.zip", FileMode.Create);
GZipStream csStream = new GZipStream(fd, CompressionMode.Compress);

byte[] compressedBuffer = new byte[500];
int offset = 0;
int nRead;

nRead = fs.Read(compressedBuffer, offset, compressedBuffer.Length);
while (nRead > 0)
{
    csStream.Write(compressedBuffer, offset, nRead);
    offset = offset + nRead;
    nRead = fs.Read(compressedBuffer, offset, compressedBuffer.Length);
}

fd.Close();
fs.Close();
Run Code Online (Sandbox Code Playgroud)

我认为确实如此,但我想解压缩上面压缩的内容.我这样想:

FileStream fd = new FileStream("g:\\gj.new", FileMode.Create);
FileStream fs = new FileStream("g:\\gj.zip", FileMode.Open);
GZipStream csStream = new GZipStream(fs, CompressionMode.Decompress);

byte[] decompressedBuffer = new byte[500]; …
Run Code Online (Sandbox Code Playgroud)

c# compression gzipstream

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

ASP.NET MVC 6上每个控制器的特定JSON设置

我需要在ASP.NET MVC 6 webApi中为每个控制器设置特定的JSON设置.我发现这个样本(我希望!)适用于MVC 5: 在ASP.NET WebAPI Per Controller上强制使用CamelCase

using System;
using System.Linq;
using System.Web.Http.Controllers;
using System.Net.Http.Formatting;
using Newtonsoft.Json.Serialization;

public class CamelCaseControllerConfigAttribute : Attribute, IControllerConfiguration 
{
    public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor)
    {
        var formatter = controllerSettings.Formatters.OfType<JsonMediaTypeFormatter>().Single();
        controllerSettings.Formatters.Remove(formatter);

        formatter = new JsonMediaTypeFormatter
        {
            SerializerSettings = {ContractResolver = new CamelCasePropertyNamesContractResolver()}
        };

        controllerSettings.Formatters.Add(formatter);

    }
}
Run Code Online (Sandbox Code Playgroud)

c# configuration json.net asp.net-core-mvc asp.net-core

13
推荐指数
2
解决办法
4757
查看次数

C# 9/10 顶级语句和 ExcludeFromCodeCoverage-Attribute?

我通常将该属性设置[ExcludeFromCodeCoverage]为我的 Program 类,因为无论如何都无法对该类进行单元测试(或者也没有意义),因此它不会在覆盖率报告中显示为“缺失”:

[ExcludeFromCodeCoverage]
public static class Program
{
    public static void Main(string[] args)
    {
       // do something awesome
    }
}
Run Code Online (Sandbox Code Playgroud)

但对于高层的声明,我不知道如何处理这个问题。正如我在这里发现的那样,似乎无法设置属性: https: //stackoverflow.com/a/69962982/1099519

到目前为止,我坚持经典的类声明,但也许他们在单元测试代码覆盖率方面考虑了其他事情?

c# code-coverage c#-9.0 toplevel-statement c#-10.0

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

使用凭据将I/O文件写入共享网络驱动器

我想在共享网络驱动器上删除.txt文件.该路径是网络驱动器上的映射,需要凭据(登录名和密码).我可以使用FileOutputStream传递这些参数吗?

FileOutputStream fos;
DataOutputStream dos;

try {
    File file= new File(path + "/" + fileName + ".txt");
    fos = new FileOutputStream(file);
    dos=new DataOutputStream(fos);
    dos.writeChars(stringContent);
    dos.close();
    fos.close();
}
catch(IOException eio){
}
Run Code Online (Sandbox Code Playgroud)

谢谢.

java filesystems file-io

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

Windows Phone 8.1位置跟踪

我想实现一个不断向Web服务发送设备位置的应用程序.查看文档,我发现了Geolocation类和一些讨论位置跟踪的文章:

实现这些文章中讨论的两个示例项目,我注意到每个位置更新都不会触发geolocator_PositionChanged()事件.两次执行事件之间有一段延迟(大约10/15分钟).奇怪的是,即使App在前台执行(不仅在后台),也会发生这种情况.我正在使用Windows Phone模拟器.

在我的应用程序中,我有一个地图控件,我需要显示用户的位置,因此,我需要为每个位置更新正确触发geolocator_PositionChanged()事件,没有延迟.

1)如何使用Geolocator类跟踪(没有延迟)设备的位置?

通过网络搜索,我找到了GeoCoordinateWatcher类,它提供了对设备的连续位置跟踪.这是代码:

public MainPage()
{
    InitializeComponent();
    this.GetCoordinate();
}

private void GetCoordinate()
{
    var watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High)
    {
        MovementThreshold = 1
    };
    watcher.PositionChanged += this.watcher_PositionChanged;
    watcher.Start();
}

private void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
    //Get position data
    var pos = e.Position.Location;
    //Update mypos object
    mypos.update(pos.Latitude, pos.Longitude);
    //Update data on the main interface
    MainMap.SetView(mypos.getCoordinate(), MainMap.ZoomLevel, MapAnimationKind.Parabolic);
}
Run Code Online (Sandbox Code Playgroud)

它的工作原理:watcher_PositionChanged()事件被触发而没有延迟.

2)为什么GeoCoordinateWatcher没有延迟?GeoCoordinateWatcher类和Geolocator类之间有什么区别?

最后,应用程序应将设备的位置发送到Web服务,即使它未处于活动状态.所以,我需要一个后台任务.正如Romasz 在这里提出的,我可以使用Geolocator类,但有一些限制.

3)我可以在后台使用GeoCoordinateWhatcher吗?如果有,怎么样?

我的目标是实现一个没有延迟的位置跟踪应用程序,甚至可以在后台运行.做这个的最好方式是什么?应用程序应跟踪设备的位置并不断更新Web服务(即使在后台).我怎样才能做到这一点?什么是最好的方法?我知道Windows Phone Apps生命周期,我可以接受后台执行的一些限制.背景限制是什么?

c# geolocation windows-runtime windows-phone-8 windows-phone-8.1

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

从通用接口继承

我不知道如何解决通用接口的问题.

通用接口表示对象的工厂:

interface IFactory<T>
{
    // get created object
    T Get();    
}
Run Code Online (Sandbox Code Playgroud)

接口代表工厂的计算机(计算机类)specyfing一般工厂:

interface IComputerFactory<T> : IFactory<T> where T : Computer
{
    // get created computer
    new Computer Get();
}
Run Code Online (Sandbox Code Playgroud)

通用接口表示对象的特殊工厂,可以克隆(实现接口System.ICloneable):

interface ISpecialFactory<T> where T : ICloneable, IFactory<T>
{
    // get created object
    T Get();
}
Run Code Online (Sandbox Code Playgroud)

类表示计算机(计算机类)和可克隆对象的工厂:

class MyFactory<T> : IComputerFactory<Computer>, ISpecialFactory<T>
{

}
Run Code Online (Sandbox Code Playgroud)

我在MyFactory类中收到编译器错误消息:

The type 'T' cannot be used as type parameter 'T' in the generic type or method 'exer.ISpecialFactory<T>'. There is no boxing conversion or type parameter …
Run Code Online (Sandbox Code Playgroud)

c# generics inheritance interface

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

具有 SAS 令牌的 StorageCredentials 的 CloudStorageAccount

遵循此 Microsoft 指南

我尝试使用sasToken凭据获取对容器的引用。

我创建了一个 sas 令牌,然后创建了凭据:(在 sas 令牌中更改了一些字母...)

public static StorageCredentials GetContainerCredentials()
{
    string sasToken = "?sv=2014-02-14&sr=c&si=read%20only%20policy&sig=JpCYrvZPXuVqlflu6BOZMh2MxfghoJt8GMDyVY7HOkk%3D";
    return new StorageCredentials(sasToken);
}
Run Code Online (Sandbox Code Playgroud)

使用凭证的代码:

public bool Init(string ContainerName, StorageCredentials credentials)
{
    try
    {
        m_containerName = ContainerName;
        CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, useHttps: true);

        if (null == storageAccount)
        {
            Console.WriteLine("storageAccount is null");
            return false;
        }

        // Create the blob client.
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        if (null == blobClient)
        {
            Console.WriteLine("blobClient is null");
            return false;
        }

        // Retrieve a reference to …
Run Code Online (Sandbox Code Playgroud)

c# azure-storage-blobs

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

如何在xml java中更新节点值

我目前正在开发一个xml项目.到目前为止,我已经使用Dom Parser成功将我的xml链接到我的java类.我在下面提供了代码.我正在努力的是将我的startdate的月份更新为1,因此像这样的2/2/2013,3/1/2013 ...将相应地在xml文件中更改.我updateDate在底部有方法调用,但是当我调用它时,xml文件不会更新它的值.帮助将不胜感激

data.xml 之前

<?xml version="1.0" encoding="UTF-8">
<data>
    <username>hello123</username>
    <startdate>01/01/2011</startdate>
    <enddate>06/01/2013</enddate>
</data>
Run Code Online (Sandbox Code Playgroud)

渴望data.xml 之后

<?xml version="1.0" encoding="UTF-8">
<data>
    <username>hello123</username>
    <startdate>02/01/2011</startdate> <--- This will change 
    <enddate>06/01/2013</enddate>
</data>
Run Code Online (Sandbox Code Playgroud)

main.java

public class main {

    public static void main(String[] args) {

        Calendar cal2 = null;

            String username = null;
            String startdate = null;
            String enddate = null;
            String date = null;
            String date_end = null;

            try {   

                File data = new File("data.xml");
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                Document …
Run Code Online (Sandbox Code Playgroud)

java xml

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

Nunit:ParallelScope Self+Children、Fixture+Children 和 All 之间的区别

有人可以解释一下应用于基本 TestFixture 时所应用的 ParallelScope 的不同组合之间的区别(多个测试组件中的所有 TestFixture 均源自它):

[TestFixture, Parallelizable(ParallelScope.All)]

[TestFixture, Parallelizable(ParallelScope.Fixtures | ParallelScope.Children)]

[TestFixture, Parallelizable(ParallelScope.Self | ParallelScope.Children)]

Nunit文档也没有提供任何说明。另外,NUnit 文档中也没有提及ParallelScope.All

我想在所有测试程序集中并行运行所有测试用例。使用其中之一可以吗?有什么区别吗?使用其中一种比另一种有什么优势?

nunit nunit-3.0

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