我需要通过反射打印出某些类型的枚举值及其相应的底层值.这在大多数情况下都可以正常工作.但是,如果枚举在泛型类型中声明,则Enum.GetValues抛出以下异常:
[System.NotSupportedException: Cannot create arrays of open type. ]
at System.Array.InternalCreate(Void* elementType, Int32 rank, Int32* pLengths, Int32* pLowerBounds)
at System.Array.CreateInstance(Type elementType, Int32 length)
at System.Array.UnsafeCreateInstance(Type elementType, Int32 length)
at System.RuntimeType.GetEnumValues()
Run Code Online (Sandbox Code Playgroud)
完整的复制代码:
using System;
public class Program
{
public static void Main()
{
var enumType= typeof(Foo<>.Bar);
var underlyingType = Enum.GetUnderlyingType(enumType);
Console.WriteLine(enumType.IsEnum);
foreach(var value in Enum.GetValues(enumType))
{
Console.WriteLine("{0} = {1}", value, Convert.ChangeType(value, underlyingType));
}
}
}
public class Foo<T>
{
public enum Bar
{
A = 1,
B = 2 …Run Code Online (Sandbox Code Playgroud) 在Unity中,有什么方法可以创建一个单独的游戏管理器,可以作为全局类在任何地方访问,静态变量会将相同的常量值吐出到每个提取这些值的类中?在Unity中实现它的方法是什么?我必须将它附加到GameObject吗?可以只是在文件夹中,而不是在视觉上在场景中吗?
我正在创建两个线程,并向它们传递一个函数,该函数执行下面显示的代码10,000,000次.
大多数情况下,"5"打印到控制台.有时它是"3"或"4".很清楚为什么会这样.
但是,它也打印"6".这怎么可能?
class Program
{
private static int _state = 3;
static void Main(string[] args)
{
Thread firstThread = new Thread(Tr);
Thread secondThread = new Thread(Tr);
firstThread.Start();
secondThread.Start();
firstThread.Join();
secondThread.Join();
Console.ReadLine();
}
private static void Tr()
{
for (int i = 0; i < 10000000; i++)
{
if (_state == 3)
{
_state++;
if (_state != 4)
{
Console.Write(_state);
}
_state = 3;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
以下代码无法使用g ++编译此错误以及与clang类似的错误.
main.cpp:16:19: error: 'QByteArray' is a private member of 'QByteArray'
Container2(const QByteArray &ba);
^
main.cpp:3:18: note: constrained by private inheritance here
class Container1:private QByteArray
^~~~~~~~~~~~~~~~~~
../edge-clang/qt5/include/QtCore/qbytearray.h:167:21: note: member is declared here
class Q_CORE_EXPORT QByteArray
^
main.cpp:18:30: error: 'QByteArray' is a private member of 'QByteArray'
Container2::Container2(const QByteArray &ba):Container1(ba){}
^
main.cpp:3:18: note: constrained by private inheritance here
class Container1:private QByteArray
^~~~~~~~~~~~~~~~~~
../edge-clang/qt5/include/QtCore/qbytearray.h:167:21: note: member is declared here
class Q_CORE_EXPORT QByteArray
^
Run Code Online (Sandbox Code Playgroud)
我不明白为什么Container2不允许继承Container1并通过公共构造函数初始化它,即使QByteArray的继承是私有的.
#include <QByteArray>
class Container1:private QByteArray
{
public:
Container1() {} …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现一个非常标准的任务:当发生异常时,重定向到我的/error页面.
在简化形式中,代码如下所示:
app.factory('$exceptionHandler', ['$location', function($location) {
return function(exception, cause) {
$location.path("/error");
};
}]);
Run Code Online (Sandbox Code Playgroud)
但是,AngularJS抱怨: 发现循环依赖:$ location < - $ exceptionHandler < - $ rootScope
这看起来像是一个基本限制,不允许$location在处理异常时使用.
但是我们还能怎么做呢?
我想将标签大小设置为与屏幕大小相同.
fe适用于大屏幕大标签,适用于小屏幕小标签.
在C#中,如何检查目录或其任何子目录中是否存在特定文件?
System.IO.File.Exists似乎只接受一个没有重载的单个参数来搜索子目录.
我可以使用SearchOption.AllDirectories重载LINQ和System.IO.Directory.GetFiles,但这看起来有点沉重.
var MyList = from f in Directory.GetFiles(tempScanStorage, "foo.txt", SearchOption.AllDirectories)
where System.IO.Path.GetFileName(f).ToUpper().Contains(foo)
select f;
foreach (var x in MyList)
{
returnVal = x.ToString();
}
Run Code Online (Sandbox Code Playgroud) 我有一个关于indexOf方法的问题.我想在字符串中找到多个"X"的情况.
假设我的字符串是"x是x是x是x",我想在其所有索引位置找到x.但是你如何针对多种情况做这件事呢?indexOf甚至可以实现这一点吗?
我做了int temp = str.indexOf('x'); 它找到了第一个x.我尝试做一个for循环,其中我被初始化为字符串的长度,这不起作用,因为我一直在寻找第一个x.
for (int y = temp1; y >= 0;y-- )
{
int temp = str.indexOf('x');
System.out.println(temp);
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用.我应该使用正则表达式吗?因为我真的不知道如何使用正则表达式方法.
任何帮助将不胜感激,谢谢!
可能重复:
计算数组中的反转
这是一个电话采访问题:"查找数组中的反转次数".我猜他们的意思是O(N log N)解决方案.我认为它不能比O(N log N)更好,因为这是排序的复杂性.
类似问题的答案可归纳如下:
a[i]找到它j在排序副本(二进制搜索)中的位置,并将距离的一半加起来abs(i - j)/2.修改merge sort:修改merge以计算两个已排序数组之间的反转,并merge sort使用修改后的数组运行merge.
是否有意义 ?还有其他(可能更简单)的解决方案吗?电话采访难道不是很难吗?