我想将func对象应用为这样的转换器:
Func<int, double> f = x => x / 2.5;
Converter<int, double> convert = f;
List<double> applied_list = new List<int>(){1, 2, 3}.ConvertAll(convert);
Run Code Online (Sandbox Code Playgroud)
编译器给我这样的消息:
错误CS0029无法将类型'System.Func'隐式转换为'System.Converter'
什么是使用功能作为转换器的最佳方法?
我有简单的类层次结构(基类A,派生类B:A,派生类C:A等).我有以下代码:
void computation(A base_class)
{
if (base_class is B)
{
//Do some stuff with (base_class as B)
}
if (base_class is C)
{
//Do some stuff with (base_class as C)
}
}
Run Code Online (Sandbox Code Playgroud)
我记得c#语言的动态关键字.据我所知,我可以使用以下代码(以优化额外的转换).
void computation(A base_class)
{
dynamic temp = base_class as B;
if (temp != null)
{
//Do some stuff with temp
}
temp = base_class as C;
if (temp != null)
{
//Do some stuff with (base_class as C)
}
}
Run Code Online (Sandbox Code Playgroud)
哪种变体更适合使用?这些方法的表现怎么样?
我有函数,它生成char对象的集合:
public static IEnumerable<char> generic_foo<T>()
{
return "1231";
}
public static IEnumerable<char> generic_foo2<T>()
{
yield return '1';
yield return '2';
yield return '3';
}
public static IEnumerable<char> foo()
{
return "1231";
}
public static IEnumerable<char> foo2()
{
yield return '1';
yield return '2';
yield return '3';
}
public static void Main()
{
var res = foo().GetType().IsGenericType; // False
var gen_res = generic_foo<int>().GetType().IsGenericType; // False
var res2 = foo2().GetType().IsGenericType; // False
var gen_res2 = generic_foo2<int>().GetType().IsGenericType; // True
}
Run Code Online (Sandbox Code Playgroud)
我对程序的结果感到疑惑.为什么结果不同?foo2/generic_foo2方法之间的主要区别是什么?
我有用C#编写的简单程序:
static void Main(string[] args)
{
int a = 0;
for (int i = 0; i < 100; ++i)
a = a + 1;
Console.WriteLine(a);
}
Run Code Online (Sandbox Code Playgroud)
我是编程领域的新手,我的目的只是了解JIT创建的汇编代码.这是一段asm代码:
7: int a = 0;
0000003c xor edx,edx
0000003e mov dword ptr [ebp-40h],edx
8: for (int i = 0; i < 100; ++i)
00000041 xor edx,edx
00000043 mov dword ptr [ebp-44h],edx
Run Code Online (Sandbox Code Playgroud)
我无法理解代码:0000003c xor edx,edx
.存储的操作结果在哪里?我在"英特尔®64和IA-32架构软件开发人员手册"中只找到了这样的引用:
逻辑指令AND,OR,XOR(异或)和NOT执行为其命名的标准布尔运算.AND,OR和XOR指令需要两个操作数; NOT指令在单个操作数上运行
编辑:据我所知,这个结果应该存储在edx
(见下一个代码行).但对我来说这似乎很奇怪.我以为结果会被推到堆栈上
我遇到了对象的PushFrame
方法Dispatcher
.它是方法的简化版本:
public void PushFrame(DispatcherFrame frame)
{
// Stuff
_frameDepth++;
while(frame.Continue)
{
// Getting and dispatching messages
}
_frameDepth--;
// Stuff
}
Run Code Online (Sandbox Code Playgroud)
换句话说,它只是打开新的消息处理循环.但我真的无法理解这种方式的好处.出于什么目的PushFrame
使用?它的用法有很好的例子吗?至于我,似乎这种方法会导致不明显的错误.
我有以下代码:
template <typename T>
struct function_traits
{
typedef decltype(&T::operator()) test_type;
typedef std::true_type res_type;
};
template <typename T>
struct function_traits
{
typedef std::false_type res_type;
};
Run Code Online (Sandbox Code Playgroud)
换句话说,我想知道类型是否具有运算符()。我以为可以使用SFINAE的方式来做到这一点。但是编译器告诉:
'function_traits':类模板已经定义。
这样的代码怎么了?
PS:这是简单的用法:
auto f1 = [](const int a){ std::cout << a << std::endl; };
function_traits<decltype(f1)>::res_type;
auto f2 = false;
function_traits<decltype(f2)>::res_type;
Run Code Online (Sandbox Code Playgroud)
编辑:我正在使用c ++ 11标准
我有这样的代码,但是编译器说,有关错误(错误C2913:明确的专业化;"向量"不是一个类模板d的专业化:\ test_folder\consoleapplication1\consoleapplication1\consoleapplication1.cpp 28 1 ConsoleApplication1):
#include <iostream>
template <int N, int ... T>
class Vector
{
public:
static void print_arguments(void)
{
std::cout << N << " : " << std::endl;
Vector<T>::print_argumetns();
}
protected:
private:
};
template <>
class Vector<>
{
public:
static void print_arguments(void)
{
}
protected:
private:
};
int main(void)
{
std::cout << "Hello world" << std::endl;
int i = 0;
std::cin >> i;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有这样的代码:
class A
{
public:
A(void);
~A(void)
{
delete b;
delete c;
delete d;
// ...
}
private:
B* b;
C* c;
D* d;
// ...
};
//A.cpp
A(void) : b(new B()), c(new C()), d(new D()) //...
{
}
Run Code Online (Sandbox Code Playgroud)
类A
取得所有权在自己的对象b
,c
,d
...什么是保持这些对象的最佳方式?我猜,std::unique_ptr<B/C/D>
类型的使用将适合这种方式.例如,它允许不关心析构函数的仔细编写.
我想用指定的泛型类型参数创建Func类型,就像这样,但只有一个语句
Type create_type(Type[] types)
{
return typeof(Func<>).MakeGenericType(types); // error if types.Length != 1
return typeof(Func<,>).MakeGenericType(types); // error if types.Length != 2
return typeof(Func<,,>).MakeGenericType(types); // error if types.Length != 3
return typeof(Func<,,,>).MakeGenericType(types); // error if types.Length != 4
// ....
}
Run Code Online (Sandbox Code Playgroud)
当然,我可以创建辅助阵列:
var func_types = new Type[] { typeof(Func<>), typeof(Func<,>) , //...}
Run Code Online (Sandbox Code Playgroud)
但这个解决方案对我来说似乎太难看了
注意:类型数组可以很长(最多15个),我想找到优雅的解决方案.不幸的是,我正在用另一种语言编写的元数据创建这种类型
我有简单的控制台应用程序(目标框架4.5.2):
using System;
public class SosTest
{
public class Foo
{
public Foo()
{
Console.WriteLine("Creation of foo");
}
}
static void Main(string[] args)
{
var n1 = new Foo();
var n2 = new Foo();
Console.WriteLine("Allocated objects");
Console.WriteLine("Press any key to invoke GC");
Console.ReadKey();
n2 = n1;
n1 = null;
GC.Collect();
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
}
Run Code Online (Sandbox Code Playgroud)
我想看看托管堆的状态.我做了以下步骤:
.load MicrosoftNet\Framework\v4.0.30319\sos.dll
!eeheap -gc
但在最后一个命令中我得到以下消息:
无法找到运行时DLL(clr.dll),0x80004005扩展命令需要clr.dll才能执行某些操作.
为什么命令!eeheap -gc
失败?
如果它会帮助它是lm
命令的结果:
0:000> lm
start …
Run Code Online (Sandbox Code Playgroud) 我只是想检查我是否GlobalReAlloc
正确理解了函数的含义。很快,GlobablRealloc
更改大小或内存对象或其属性。我有以下问题:
HGlobal old_mem = GlobalAlloc(...);
HGlobal new_mem = GlobalReAlloc(12345, old_mem, 0); // Should I free old_mem?
Run Code Online (Sandbox Code Playgroud)
我想了解IsGenericTypeDefinition
Type的属性.据我所知,它只是没有任何替换类型的类模板(我们无法创建此类的实例).这是我的测试程序的代码:
class Test
{
public class MyList<T> : List<T>
{
}
public static void Main()
{
Console.WriteLine(typeof(MyList<>).GetMethod("Add").DeclaringType.IsGenericTypeDefinition);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么打印程序false
?我只是从泛型类型定义中获取此方法信息.为什么它没有通用的声明类型定义?是bug吗?
我有以下程序:
public class TestClass
{
[Conditional("DEBUG")]
static void debug_foo()
{
Console.WriteLine("DEBUG foo");
}
static int Main(System.String[] args)
{
debug_foo();
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
我使用发布模式编译程序.我在程序集中找到了debug_foo方法的il代码.我无法理解这种解决方案的原因.我认为如果c#编译器可以理解,那个方法没有使用,那么编译器根本不需要生成il代码(例如,它不会在main函数中生成调用指令),因为它会减慢编译速度并增加汇编的大小.