小编ace*_*33s的帖子

如何将List <interface>转换为List <concrete>?

我有一个接口定义为:

public interface MyInterface {
     object foo { get; set; };
}
Run Code Online (Sandbox Code Playgroud)

以及实现该接口的类:

public class MyClass : MyInterface {
     object foo { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后我创建一个返回ICollection的函数,如下所示:

public ICollection<MyClass> Classes() {
    List<MyClass> value;

    List<MyInterface> list = new List<MyInterface>(
        new MyInterface[] {
            new MyClass {
                ID = 1
            },
            new MyClass {
                ID = 1
            },
            new MyClass {
                ID = 1
            }
        });

    value = new List<MyClass>((IEnumerable<MyClass>) list);

    return value;
}
Run Code Online (Sandbox Code Playgroud)

它会编译,但会抛出一个

无法转换'System.Collections.Generic.List 1[MyInterface]' to type 'System.Collections.Generic.IEnumerable1 [MyClass]' 类型的对象. …

c# generics interface

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

VB.NET RemoveHandler和匿名方法

如何使用RemoveHandler匿名方法?

这是我为MyEvent类的事件添加处理程序的方法MyClass:

AddHandler MyClass.MyEvent, Sub()
                                '...
                            End Sub
Run Code Online (Sandbox Code Playgroud)

然后我如何使用RemoveHandler删除MyEvent事件的处理程序?

vb.net anonymous-methods addhandler

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

Global.asax.cs无法访问App_Code中的类

我在App_Code中创建了一个新类

namespace Site {
    public class MyClass {
        public MyClass() {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的Global.asax.cs

namespace Site {
    public class Global : System.Web.HttpApplication {
        protected void Application_Start(object sender, EventArgs e) {
            *MyClass myClass = new MyClass();*
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

错误发生在:MyClass myClass = new MyClass();

找不到类型或命名空间名称'MyClass'(您是否缺少using指令或程序集引用?)

我在这里错过了什么?

asp.net app-code global-asax

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

用抽象方法显式实现接口

这是我的界面:

public interface MyInterface {
    bool Foo();
}
Run Code Online (Sandbox Code Playgroud)

这是我的抽象类:

public abstract class MyAbstractClass : MyInterface {
    abstract bool MyInterface.Foo();
}
Run Code Online (Sandbox Code Playgroud)

这是编译器错误: "修饰符'abstract'对此项无效.

我应该如何继续使用抽象方法显式实现抽象?

c# interface abstract

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

如何在C#中覆盖枚举?

这是我的代码:

void Main()
{
    StarTrek baseClass = new StarTrek();
    NewGeneration childClass = new NewGeneration();

    baseClass.ShowEnum();
    childClass.ShowEnum();
}

public class StarTrek
{
    internal enum Characters
    {
        Kirk, Spock, Sulu, Scott
    }

    public void ShowEnum()
    {
        Type reflectedEnum = typeof(Characters);
        IEnumerable<string> members = reflectedEnum.GetFields()
                                            .ToList()
                                            .Where(item => item.IsSpecialName == false)
                                            .Select(item => item.Name);
        string.Join(", ", members).Dump();
    }
}

public class NewGeneration : StarTrek
{
    internal new enum Characters
    {
        Picard, Riker, Worf, Geordi
    }       
}
Run Code Online (Sandbox Code Playgroud)

ShowEnum始终显示:

柯克,斯波克,苏禄,斯科特

即使它是在NewGeneration类中调用的.我错过/误解了什么吗?ShowEnum有没有办法使用NewGeneration.Characters而不是StarTrek.Characters?

c# enums overloading

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

LINQ to SQL不同版本的SQL Server之间的兼容性

我通过连接到SQL Server 2005数据库创建了我的DataClasses .如果我的应用程序要连接到SQL Server 2000数据库,是否需要重新编译?

SQL Server 2000不支持ROW_NUMBER()函数.我正在使用LINQ to SQL的Skip()和Take()方法来浏览我的记录.LINQ to SQL是否会正确生成正确的SQL查询,尽管它被编译为连接到较新版本的SQL Server?

.net sql-server compatibility linq-to-sql

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

PowerShell:如何删除名称以点开头的文件夹(例如".svn")

试过这个:

Remove-Item "C:\foo\.svn"
Run Code Online (Sandbox Code Playgroud)

但这是我遇到的错误

Remove-Item : Cannot find path 'C:\foo\.svn' because it does no
t exist.
At line:1 char:12
+ Remove-Item <<<<  "C:\foo\.svn"
    + CategoryInfo          : ObjectNotFound: (C:\foo\.svn:String) [Remove-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
Run Code Online (Sandbox Code Playgroud)

powershell

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