我需要通过反射打印出某些类型的枚举值及其相应的底层值.这在大多数情况下都可以正常工作.但是,如果枚举在泛型类型中声明,则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) 最近的一个问题让我重新思考这整个辅助类是反模式的东西.
asawyer在评论中指出了一些关于这个问题的链接: Helper 类 是一种 反模式.
虽然这些链接详细说明了辅助类如何与众所周知的oop原则相冲突,但有些事情仍然不清楚.
例如"不要重复自己".如何在不创造某种帮助的情况下取得成功呢?我认为你可以派生出某种类型并为它提供一些功能.但我觉得这并不是一直很实用.
让我们看一下下面的例子,请记住我尽量不使用任何更高级的语言功能,也不使用"特定于语言"的东西.所以这可能是丑陋的嵌套而不是最佳......
//Check if the string is full of whitepsaces
bool allWhiteSpace = true;
if(input == null || input.Length == 0)
allWhiteSpace = false;
else
{
foreach(char c in input)
{
if( c != ' ')
{
allWhiteSpace = false;
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
让我们创建一个名为StringHelper的错误辅助类,代码变得更短:
bool isAllWhiteSpace = StringHelper.IsAllWhiteSpace(input);
Run Code Online (Sandbox Code Playgroud)
所以,因为这不是我们唯一需要检查的时间,我想这里是"不要重复自己".
我们如何在没有帮助的情况下取得成功?考虑到这段代码不受单个类的约束?
我们需要继承字符串并将其称为BetterString吗?
bool allWhiteSpace = better.IsAllWhiteSpace;
Run Code Online (Sandbox Code Playgroud)
还是我们创建一个类?StringChecker
StringChecker checker = new StringChecker();
bool allWhiteSpace = checker.IsAllwhiteSpace(input);
Run Code Online (Sandbox Code Playgroud)
那我们怎么做到这一点呢?
某些语言(例如C#)允许使用ExtensionMethods.他们算作助手吗?我倾向于喜欢那些超过辅助课程的人.
我们最近在Visual Studio 2017中从头开始安装了一个新的开发环境(Windows 10),但我们无法设法msbuild
编译Microsoft Office Addin for Word(vsto).在Visual Studio安装程序中,我们确保包含Office Development所需的组件.
它抛出以下错误(德语翻译成英语)
错误MSB4226:找不到导入的项目"C:\ Program Files(x86)\ Microsoft Visual Studio\2017\BuildTools\MSBuild\Microsoft\VisualStudio\v15.0\OfficeTools\Microsoft.VisualStudio.Tools.Office.targets".此外,尝试在$(VSToolsPath) - "C:\ Program Files(x86)\ MSBuild\Microsoft\VisualStudio\v15"的后备搜索路径中找到"OfficeTools\Microsoft.VisualStudio.Tools.Office.targets". 0"[...]
但是,该项目在Visual Studio中构建良好.
在旧系统上,一切正常,我不记得必须配置任何东西.
最近,我在构建WPF项目的Visual Studio 2012中遇到了一个奇怪的问题.
当我安装了视频驱动程序时,在启动Debug时加载XAML Viewer和WPF应用程序本身需要很长时间.这也适用于exe文件.
卸载视频驱动程序(使用基本MS驱动程序)时,一切运行正常.
我已经为我的NVIDIA 8700m GT尝试了不同的驱动程序版本(赢得7 Professular 64bit)但没有成功.
有谁知道发生了什么?
我不知道这里的问题是什么,
我有一个HashSet<object>
名为_itemsToProcess的人.
最终在我的代码中(没有多头)我想从hashset中删除项目
_itemsToProcess.Remove(item);
Run Code Online (Sandbox Code Playgroud)
这不起作用.我也试过了
_itemsToProcess.RemoveWhere(i=>i.Equals(item));
Run Code Online (Sandbox Code Playgroud)
嗯,它看起来微不足道,但我可以保证,项目在_itemsToProcess内.我通过调试检查了
_itemsToProcess.Any(item.Equals) // Returns true
_itemsToProcess.Contains(item) // Returns false
item.GetHashcode() == _itemsToProcess.First().GetHashcode() // returns true aswell.
Run Code Online (Sandbox Code Playgroud)
该项目没有实现ICompareable,也没有IEquatable,所以我在这里想法.
示例(简化了很多,因为这是一件大事)
readonly _itemsToProcess = new HashSet<object>();
void getItems()
{
foreach(object o in getObjects())
if(meetsCriteria(o)) _itemsToProcess.Add(o);
}
void processItems()
{
if(_itemsToProcess.Count> 0)
{
foreach(object item in _itemsToProcess.Where(criteria).ToArray())
processItem(item);
}
}
// This gets called in different ways
void processItem(object item)
{
... do stuff
if(succes)
_itemsToProcess.Remove(item);
}
Run Code Online (Sandbox Code Playgroud)
所以重新解决问题
var compare = _itemsToProcess.First();
compare.GetHashcode() == item.GerHashcode() // …
Run Code Online (Sandbox Code Playgroud) 在OpenGL中进行旋转和转换时我有点卡住了.
我有3个矩阵,投影,视图和模型.
我的VertexShader:
gl_Position = projection * model * view * vec4(vertexData, 1);
Run Code Online (Sandbox Code Playgroud)
翻译和旋转对象的最佳方法是什么?
要么将我的模型矩阵与平移和/或旋转矩阵相乘,要么将数据(旋转和平移)传递给着色器和那里的数学?
我还需要知道我的鼠标实现的"最终对象位置".
到目前为止我做的是这样的:
object.Transformation = Matrix.CreateTransLation(x,y,z) * Matrix.CreateRotation(x,y,z);
Run Code Online (Sandbox Code Playgroud)
...
ForEach object to Draw
{
modelMatrix.Push();
modelMatrix.Mult(object.Transformation); // this also updates the matrix for the shader
object.Draw();
modelMatrix.Pop();
}
Run Code Online (Sandbox Code Playgroud)
这有效,但感觉不对.最好的方法是什么?
调试代码时,有时会出现这个奇怪的错误。当我在调试时更改代码中的某些内容时,就会发生这种情况。
这与框架没有真正的联系,因为到目前为止,这是在不同版本中发生的。
从德语翻译:
Error 1 The sourcefile "C:\Users\xxxx\AppData\Local\Temp\8\.NETFramework,
Version=v4.5.1.AssemblyAttributes.cs"
could not be opened("Unknown Error"). folderOfProjectFile
Run Code Online (Sandbox Code Playgroud)
整个目录“ 8”不存在,appdata\local
只有“ 4”
附加信息:
搜索此错误消息时,Google提供的服务不多。主要是因为我的翻译可能不完全正确。
这是原始的例外
Fehler 3 Die Quelldatei "C:\Users\XXXX\AppData\Local\Temp\5\.NETFramework,
Version=v4.5.1.AssemblyAttributes.cs"
konnte nicht geöffnet werden ("Unbekannter Fehler "). folderOfProjectfile
Run Code Online (Sandbox Code Playgroud)
有谁知道会导致这个问题的原因吗?
在查看List.AddRange的实现时,我发现了一些奇怪的东西,我不明白. 源代码,请参阅第727行(AddRange调用InsertRange)
T[] itemsToInsert = new T[count];
c.CopyTo(itemsToInsert, 0);
itemsToInsert.CopyTo(_items, index);
Run Code Online (Sandbox Code Playgroud)
为什么要将它首先复制到"temp-array"(itemsToInsert)中,然后将temp数组复制到实际的_items-array中?这背后是否有任何原因,或者这只是复制ArrayList源码的剩余部分,因为同样的事情发生在那里.
我有一些代码接受IEnumerable并从中生成Excel文档.IEnumerable中的对象有一个日期字段,我希望这些对象在Excel中被格式化为日期.但是,当您在Excel中查看它时,日期似乎不是"日期"数据类型,直到您双击单元格,然后按Enter键.执行此操作时,值将移至右对齐,表过滤器可正常工作.如果您没有双击并输入事物,则该值左对齐,表格过滤器将其视为文本而不是日期.我需要Excel将其视为开箱即用的日期.
这是我的代码.
/// <summary>
/// Converts any IEnumerable to an Excel Document. Objects appear in the order declared in the class.
/// </summary>
/// <typeparam name="T">Any object.</typeparam>
/// <param name="objects">List of objects to generate document from.</param>
/// <returns>Byte array representing a .xlsx file.</returns>
public static byte[] ToExcelDocument<T>(this IEnumerable<T> objects)
{
int currentrow = 1;
Type type = typeof(T);
List<PropertyInfo> propertyinfos = type.GetProperties().ToList();
int numcolumns = propertyinfos.Count;
ExcelPackage pck = new ExcelPackage();
ExcelWorksheet ws = pck.Workbook.Worksheets.Add(type.Name + "(s)");
for (int i …
Run Code Online (Sandbox Code Playgroud) 有没有人知道在使用CSharpCompilation的extensionmethod Emit发射程序集时是否可以指定Framework-Version?
我们需要明确地定位4.5.1,否则我们会得到一个讨厌的运行时错误:
MissingMethodException
Method not found: '!!0[] System.Array.Empty()'.
Run Code Online (Sandbox Code Playgroud)
或者我们是否必须等到客户升级到Framework 4.6才能使用Roslyn?