Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无约束旋律误差_C#_C# 4.0_.net 4.0_Unconstrained Melody - Fatal编程技术网

C# 无约束旋律误差

C# 无约束旋律误差,c#,c#-4.0,.net-4.0,unconstrained-melody,C#,C# 4.0,.net 4.0,Unconstrained Melody,我刚刚为Jon Skeet的项目安装了nuget软件包,但当我尝试使用它时,在编译时出现了一个错误: 类型参数“T”继承冲突约束“UnconstrainedMelody.IEnumConstraint”和“System.ValueType” 功能定义: public void SetEnum<T>() where T : struct, IEnumConstraint {} public void SetEnum(),其中T:struct,IEnumConstraint{} 我错

我刚刚为Jon Skeet的项目安装了nuget软件包,但当我尝试使用它时,在编译时出现了一个错误:

类型参数“T”继承冲突约束“UnconstrainedMelody.IEnumConstraint”和“System.ValueType”

功能定义:

public void SetEnum<T>() where T : struct, IEnumConstraint {}
public void SetEnum(),其中T:struct,IEnumConstraint{}

我错过什么了吗?我应该不使用nuget包吗?

我可能是错的,但是,虽然这个库在内部使用了
IEnumConstraint
,并使它能够处理本文中描述的后期构建步骤,但它并没有为您直接为自己的方法使用
IEnumConstraint
提供任何魔力

本文中描述的
GetValues
方法是
UnconstrainedMelody.Enums
类提供的几种方法之一。还有其他可用的对象和方法


如果您想将自己的泛型方法约束到枚举,那么可以按照Jon用于构建此库的相同步骤,但是在您自己的库上。在关于如何使用PostSharp执行此操作的评论中也出现了一些问题。

我还没有尝试过,但您似乎可以使用此MSBuild任务来完成与ConstraintChanger相同的任务。您必须在项目中包含DelegateConstraint.cs和IEnumConstraint.cs代码文件的副本。将生成任务应用于项目后,约束将被调出,引用此项目的其他项目将能够看到约束

因此,它对于在解决方案中创建公共库项目非常有用,并且可以包括自定义泛型扩展方法之类的内容,这些方法的类型受System.Enum和System.Delegate的约束

所有贷记人:j..。@friesen.us

I've found this project to be useful but wanted to have the build-time steps in MSBuild.  Adding this to your *.csproj files in which you use the constraints should accomplish the same thing as the Constraintchanger app.  Note the following:

1. I've added the two constraint types to my classes root namespace and the substitution looks for the types in the assembly's root namespace.

2. To make Resharper happier I've added the IEnumConstraint, DelegateConstraint type args into my project inside an #if UNCONSTRAINED ... #endif block like so:

public static T Parse<T>(string val) where T : struct
#if UNCONSTRAINED
, IEnumConstraint
#endif
{

}

This is purely optional but keeps resharper from complaining about the constraint not matching when using the project's code from another project in a common solution.

  <PropertyGroup>
    <BuildDependsOn>
      $(BuildDependsOn);
      SwapConstraints
    </BuildDependsOn>
  </PropertyGroup>
  <ItemGroup>
    <PreprocessorDefines Include="UNCONSTRAINED" />
  </ItemGroup>
  <UsingTask TaskName="FileReplace" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
    <ParameterGroup>
      <FileName ParameterType="System.String" Required="true" />
      <Source ParameterType="System.String" Required="true" />
      <Replacement ParameterType="System.String" Required="true" />
    </ParameterGroup>
    <Task>
      <Code Type="Fragment" Language="cs"><![CDATA[
string content = File.ReadAllText(FileName);
content = content.Replace(Source, Replacement);
File.WriteAllText(FileName, content);

]]></Code>
    </Task>
  </UsingTask>
  <Target Name="SwapConstraints">
    <GetFrameworkPath>
      <Output TaskParameter="Path" PropertyName="FW" />
    </GetFrameworkPath>
    <GetFrameworkSdkPath>
      <Output TaskParameter="Path" PropertyName="SDK" />
    </GetFrameworkSdkPath>
    <PropertyGroup>
      <ILDASM>"$(SDK)bin\NETFX 4.0 Tools\ildasm.exe"</ILDASM>
      <ILASM>"$(FW)\ilasm.exe"</ILASM>
      <IlFile>$(OutputPath)$(AssemblyName).il</IlFile>
      <DllFile>$(OutputPath)$(AssemblyName).dll</DllFile>
    </PropertyGroup>
    <Exec Command="$(ILDASM) /OUT=$(IlFile) $(DllFile)" WorkingDirectory="$(ProjectDir)" />
    <FileReplace FileName="$(IlFile)" Source="($(RootNamespace).DelegateConstraint)" Replacement="([mscorlib]System.Delegate)" />
    <FileReplace FileName="$(IlFile)" Source="([mscorlib]System.ValueType, $(RootNamespace).IEnumConstraint)" Replacement="([mscorlib]System.Enum)" />
    <FileReplace FileName="$(IlFile)" Source="($(RootNamespace).IEnumConstraint), [mscorlib]System.ValueType" Replacement="([mscorlib]System.Enum)" />
    <FileReplace FileName="$(IlFile)" Source="($(RootNamespace).IEnumConstraint)" Replacement="([mscorlib]System.Enum)" />
    <Exec Command="$(ILASM) /OUTPUT=$(DllFile) /DLL $(IlFile)" WorkingDirectory="$(ProjectDir)" />
  </Target>

Nov 22, 2013
#1 j...@friesen.us

Sorry, didn't mean to say #if UNCONSTRAINED ... #endif should be around type args for DelegateConstraint.  I've not messed with delegates to this point but I doubt that it would be necessary for them.
我发现此项目很有用,但希望在MSBuild中包含构建时步骤。将其添加到使用约束的*.csproj文件中,应完成与Constraintchanger应用程序相同的任务。注意以下几点:
1.我已将这两个约束类型添加到我的类根命名空间中,替换将在程序集的根命名空间中查找这些类型。
2.为了让Resharper更开心,我添加了IEnumConstraint,DelegateConstraint类型args到我的项目中,在一个#if unconstraint#endif块,如下所示:
公共静态T解析(字符串val),其中T:struct
#如果不受约束
,IEnumConstraint
#恩迪夫
{
}
这纯粹是可选的,但可以防止resharper在公共解决方案中使用来自另一个项目的项目代码时抱怨约束不匹配。
$(BuildDependsOn);
SwapConstraints

“$(SDK)bin\NETFX 4.0工具\ildasm.exe”
“$(FW)\ilasm.exe”
$(OutputPath)$(AssemblyName).il
$(OutputPath)$(AssemblyName).dll
2013年11月22日
#1 j.。@friesen.us
对不起,我不是故意说“如果不受约束#endif应该在DelegateConstraint的args类型附近。到目前为止,我还没有和代表们发生冲突,但我怀疑这对他们来说是必要的。
将“无约束的旋律”的思想融入到音乐中

它附带了必要的工具,将IL编织纳入构建过程,并且正在积极维护。因此,与其使用“无约束旋律”中的代码,不如使用此代码。对于枚举,还提供了其他改进


ExtraConstraints还支持向委托添加约束。

我真的很想为这个问题创建一个
jon skeet
标记,但我推迟了…在阅读文章时,我看到T不能是System.ValueType,那么约束怎么会冲突呢???^^谷歌上该项目的上一个版本是2009年。。。可能是新版本的c#破坏了它?看起来像是编译器中的一个bug。@Nathan-它是在2010年11月为VS2010更新的,Nuget软件包是从2011年8月开始的。(). 所以它应该有用。有趣。我想你可能是对的。这可以解释
,但是如果你想使用“约束转换器”,你需要从这个网站下载并构建源代码。
谷歌代码项目页面上的行。如果是这样的话,我肯定误解了这个图书馆的目的。@Bobson-在你指出之前,我并不知道这个图书馆,我最初也得出了同样的结论。Nuget包的描述更准确。这篇博文更多的是关于它是如何工作的,而不是它的作用。