小编Ken*_*Kin的帖子

什么编译器考虑switch语句?

从一个-5问题再次启发!

我读了@Quartermeister的 [ 评论 ] 并感到惊讶!

那么为什么这会编译

switch(1) {
    case 2:
}
Run Code Online (Sandbox Code Playgroud)

但事实并非如此.

int i;

switch(i=1) {
    case 2: // Control cannot fall through from one case label ('case 2:') to another
}
Run Code Online (Sandbox Code Playgroud)

这不是

switch(2) {
    case 2: // Control cannot fall through from one case label ('case 2:') to another
}
Run Code Online (Sandbox Code Playgroud)

更新:

-5问题变得-3.

c# switch-statement unreachable-code

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

什么时候stackoverflow公平合理?

代码已更新

为了修复过滤的错误Interminable,以下代码更新并合并为原始:

public static bool IsInfinity(this IEnumerable x) {
    var it=
        x as Infinity??((Func<object>)(() => {
            var info=x.GetType().GetField("source", bindingAttr);
            return null!=info?info.GetValue(x):x;
        }))();

    return it is Infinity;
}
Run Code Online (Sandbox Code Playgroud)

bindingAttr 被宣布为常数.


  • 摘要

    我正在尝试实现一个无限的可枚举,但遇到的东西似乎是不合逻辑的,暂时没有想法.我需要一些方向来完成代码,成为一个语义,逻辑和合理的设计.

  • 整个故事

    几个小时前我问过这个问题:

    无数可数仍然是"可枚举的"?

    这可能不是一个好的实施模式.我正在尝试做的是,以逻辑和语义的方式实现一个可枚举的无穷大(我想......).我会把代码放在这篇文章的最后.

    大问题是,它只是无限枚举呈现,但实际上它枚举没有任何意义,因为是它没有真正的元素.

    因此,除了为枚举提供虚拟元素外,还有四个我能想象的选项,其中三个导致了StackOverflowException.

    1. 抛出InvalidOperationException一次它将被枚举.

      public IEnumerator<T> GetEnumerator() {
          for(var message="Attempted to enumerate an infinite enumerable"; ; )
              throw new InvalidOperationException(message);
      }
      
      Run Code Online (Sandbox Code Playgroud)
    2. 和3是技术上等同,让当它出现堆栈溢出的真正溢出.

      public IEnumerator<T> GetEnumerator() {
          foreach(var x in this)
              yield return …
      Run Code Online (Sandbox Code Playgroud)

c# ienumerable infinite

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

如何找到两种类型中最小的可分配类型(重复)?

这是两种使用的扩展方法

public static Type FindInterfaceWith(this Type type1, Type type2) {
    // returns most suitable common implemented interface
}

public static Type FindBaseClassWith(this Type type1, Type type2) {
    // returns most derivative of common base class
}
Run Code Online (Sandbox Code Playgroud)
  • FindInterfaceWith返回null如果他们没有共同实现的接口.
  • FindBaseClassWith返回System.Object,如果他们有没有更多的衍生物共同的基类.
  • FindBaseClassWithnull如果其中一个参数是一个接口,则返回
  • null如果有任何参数,它们都会返回null.

最终解决方案中的方法签名如下:

public static Type FindAssignableWith(this Type type1, Type type2) {
    // what should be here?
}
Run Code Online (Sandbox Code Playgroud)

反射和Linq仅限使用,除非没有其他方法.

有没有好的方法之间找到共同类型的最适合type1type2

或者有更好的事情来实现这一目标吗?


更新:

根据我个人的理解,由于能够使用类实现多个接口, …

c# reflection types

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

如何在Windows Phone 8(C#)框架中添加C++ DLL

我试图Windows Phone 8在Visual Studio Express 2012中向框架添加C++ DLL .

我试过以下方法

  1. 通过PInvoke导入和调用

    [DllImport("WP8DLL.dll",CallingConvention = CallingConvention.Cdecl)]

    public static extern int functionReturningInteger();

    结果:这种方式虽然没有编译错误,但是当我尝试访问它抛出的DLL的方法时System.NotSupportedException.

  2. 在项目属性中添加引用

    结果:我收到消息"无法将对更高版本或不兼容程序集的引用添加到项目中 "

c# dll dllimport visual-studio-2012 windows-phone-8

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

"return"关键字有什么特别之处

当我似乎理解Haskell的回归时,我尝试使用不同的替代方案,似乎返回不仅可以在monad链中的任何地方使用,而且可以完全排除

*Main> Just 9 >>= \y -> (Just y) >>= \x -> return x
Just 9

*Main> Just 9 >>= \y -> (return y) >>= \x -> (Just y)
Just 9

*Main> Just 9 >>= \y -> (Just y) >>= \x -> (Just x)
Just 9 
Run Code Online (Sandbox Code Playgroud)

即使我在自己的实例中省略了返回,我也只会收到警告......

data MaybeG a = NothingG | JustG a deriving Show 
instance Monad MaybeG where  
    --    return x = JustG x  
        NothingG >>= f = NothingG  
        JustG x >>= f  = f x  
        fail …
Run Code Online (Sandbox Code Playgroud)

monads haskell functional-programming

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

用于流畅API的类型推断

我有以下扩展方法:

public static IFoo Foo(this IFluentApi api, Action action);

public static IFoo<TResult> Foo<TResult>(
    this IFluentApi api, Func<TResult> func);

public static IBar Bar(this IFoo foo);

public static void FooBar(this IBar bar, Action action);

public static void FooBar<TResult>( // <- this one cannot work as desired 
    this IBar bar, Action<TResult> action);
Run Code Online (Sandbox Code Playgroud)

通用接口始终从其对应的非通用接口派生.

不幸的是,为了使这项工作:

api.Foo(x => ReturnLong())
   .Bar()
   .FooBar(x => ...); // x should be of type long
Run Code Online (Sandbox Code Playgroud)

我还需要实现以下扩展方法:

public static IBar<TResult> Bar<TResult> (this IFoo<TResult> foo);
Run Code Online (Sandbox Code Playgroud)

并将上述最后一个扩展方法更改为:

public static void FooBar<TResult>(
    this IBar<TResult> …
Run Code Online (Sandbox Code Playgroud)

c# generics fluent-interface fluent

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

无法使用集合初始值设定项实现XYZ类型,因为它没有实现'System.Collections.IEnumerable'

我有以下课程:

public class CommentList 
{
    string ItemType;
    string Comment1;
    string Status1;
    string DiscussionBoardId;
    Guid CourseId;
    Guid CommentID;
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试执行以下LINQ语句:

List<CommentList> query=
    from c in db.Comments
    join s in db.Status on c.StatusId equals s.StatusId
    join d in db.DiscussionBoards 
        on c.DiscussionBoardId equals d.DiscussionBoardId
    where d.CourseId=="CourseID"
    orderby d.ItemType, d.DiscussionBoardId
    select new CommentList {
        d.ItemType,
        c.Comment1,
        s.Status1,
        c.DiscussionBoardId,
        d.CourseId,
        c.CommentID
    };
Run Code Online (Sandbox Code Playgroud)

问题是,编辑器抱怨select语句的第一个括号.它说:

无法使用集合初始值设定项实现类型"CommentList",因为它没有实现"System.Collections.IEnumerable".

有人可以帮助我,告诉我我做错了什么吗?

c# linq asp.net collections

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

提高分割文件的速度

我正在使用此代码从文件中提取块

// info is FileInfo object pointing to file
var percentSplit = info.Length * 50 / 100; // extract 50% of file
var bytes = new byte[percentSplit];
var fileStream = File.OpenRead(fileName);
fileStream.Read(bytes, 0, bytes.Length);
fileStream.Dispose();
File.WriteAllBytes(splitName, bytes);
Run Code Online (Sandbox Code Playgroud)

有没有办法加快这个过程?

目前,对于530 MB的文件,它需要大约4 - 5秒.这次可以改善吗?

c# filestream file-copying

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

如何在类声明上实现伪经典继承?

注意:

正如答案所说,由于问题和我的评论中描述的一些问题,问题中提出的代码并没有真正实现继承(否则它会成为答案而不是问题......).它可以像预期的那样继承(甚至不是原型).


  • 摘要

    简而言之,使它类似于我们编写一般的OO语言而不是javascript,但保持继承是正确的.

  • 故事

    的Object.create是实现原型继承的好方法,但它是一个有点混乱,以一个类型的大脑和新的球迷.

    我们可以通过各种方式编写javascript代码,就像我们使用伪经典模式编写其他OO语言一样.因为它是经典的,我们必须正确处理javascript的底层原型继承.

    我想要找到的是一种可以在类声明上实现伪古典继承的方法.演示代码放在帖子的后面,它按预期工作,但是,有一些烦人的事情:

    1. 我无法摆脱return类声明或继承不起作用.

    2. 除了传入this类声明以使返回的闭包知道是什么,我没办法this.

    3. 我也想摆脱function (instance, _super) {,但还没有一个好主意.

    4. 不继承类的静态(自己的属性).

    解决方案比现有框架更多的是一些语法糖,一个好的模式是适用的.


_extends函数:

function _extends(baseType) {
    return function (definition) {
        var caller=arguments.callee.caller;
        var instance=this;

        if(!(instance instanceof baseType)) {
            (caller.prototype=new baseType()).constructor=caller;
            instance=new caller();
        }

        var _super=function () {
            baseType.apply(instance, arguments);
        };

        definition(instance, _super);
        return instance;
    };
}
Run Code Online (Sandbox Code Playgroud)

Abc类:

function Abc(key, …
Run Code Online (Sandbox Code Playgroud)

javascript inheritance types prototype

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

如何使纹理始终面对相机..?

更新5

创建了另一个小提琴以显示预期的外观。添加了不可见的穹顶和魔方相机,并使用了环境贴图;就我而言,由于已经提到的原因,不应使用这些技术。

var MatcapTransformer = function(uvs, face) {
  for (var i = uvs.length; i-- > 0;) {
    uvs[i].x = face.vertexNormals[i].x * 0.5 + 0.5;
    uvs[i].y = face.vertexNormals[i].y * 0.5 + 0.5;
  }
};

var TransformUv = function(geometry, xformer) {
  // The first argument is also used as an array in the recursive calls 
  // as there's no method overloading in javascript; and so is the callback. 
  var a = arguments[0],
    callback = arguments[1];

  var faceIterator = function(uvFaces, index) {
    xformer(uvFaces[index], …
Run Code Online (Sandbox Code Playgroud)

javascript geometry texture-mapping uv-mapping three.js

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