我正在使用VS2008,并希望根据属性上的自定义属性创建编译时警告/错误(如果可能).
目前有两个案例让我感兴趣:
[MyAttribute (typeof(MyClass)]
Run Code Online (Sandbox Code Playgroud)
MyClass必须实现一个接口.目前我在属性的构造函数中声明了这一点,但由于堆栈跟踪的性质,这不容易跟踪:
public MyAttribute (Type MyClassType)
{
System.Diagnostics.Debug.Assert(typeof(MyInterface).IsAssignableFrom(MyClassType),
"Editor must implement interface: " + typeof(MyInterface).Name);
}
Run Code Online (Sandbox Code Playgroud)
我感兴趣的第二种情况是我在属性中定义了一个类型,如果该类型实现了一个接口,那么如果另一个属性不存在则应该显示警告.
IE if(MyClass.Implements(SomeInterface)&&!Exists(SomeAttibute)){Generate Warning}
[MyAttribute(typeof(MyClass)]
// Comment next line to generate warning
[Foo ("Bar")]
Run Code Online (Sandbox Code Playgroud)
谢谢!
我目前有一个功能:
public static Attribute GetAttribute(MemberInfo Member, Type AttributeType)
{
Object[] Attributes = Member.GetCustomAttributes(AttributeType, true);
if (Attributes.Length > 0)
return (Attribute)Attributes[0];
else
return null;
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否值得将属性上的所有属性缓存到
Attribute = _cache[MemberInfo][Type]
字典中,
这将需要在GetCustomAttributes
没有任何类型参数的情况下使用,然后枚举结果.这值得么?
我正在寻找一种方法来实现C#中HLSL中的swizzle功能,对于那些不熟悉它的人来说 - 它用于简单的向量元素访问.
Vector4 v1, v2;
// (t,x,y,z) or (alpha,r,g,b)
v1 = new Vector4 (1,2,0,0);
v2 = new Vector4 (0,0,3,4);
// v1 (Green, Z) = v2 (Y, Blue)
v1.gz = v2.yb;
// Result : v1 = (1,2,3,4)
Run Code Online (Sandbox Code Playgroud)
可以创建大量属性(每种可能的组合一个).我觉得有可能通过Linq做,但我真的没有太多经验.
我不知道XNA是否有类似的东西,但我不想沿着这条路走下去,因为这就是我将要使用的所有东西,也就是说,如果有的话.
谢谢.
C#4.0带来了可选参数,我已经等了很长时间了.但是,似乎因为只有System类型可以const
,我不能使用我创建的任何类/结构作为可选参数.
有没有一种方法可以让我使用更复杂的类型作为可选参数.或者这是人们必须忍受的现实之一?
我有一些麻烦传递对一般类型的对象的引用.我找到了一种方法,创建一个'对象'并传递一个引用而不是原始的 - 但它似乎闻到了一点我.这里有更好的方式,还是我必须忍受它?
我理解第一个错误,但第二个错误让我失望.
public static T Foo<T>(ref T Bar)
{
T Result;
// Next line gives
// cannot convert from 'ref T' to 'ref object'
Result = (T)ModifyObject (ref Bar);
// Next line gives
// A ref or out argument must be an assignable variable
Result = (T)ModifyObject (ref ((Object)Bar) );
// Works
Object Tmp = Bar;
Result = (T)ModifyObject (ref Tmp) );
return Result;
}
public static Object DoSomthing(ref Object Obj) {
Object Result = Activator.CreateInstance (Obj.GetType ()) …
Run Code Online (Sandbox Code Playgroud) 我想知道为什么单个隐式转换为枚举值的方式与转换为系统类型时的方式不同.我看不出任何技术上的原因,但也许有人比我更聪明,能给我带来一些启示.
后续编译无法编译,"A value of an integral type expected"
和"Cannot implicitly convert type 'Test.En' to 'Test.Foo"
.
void test1 (){
Foo f = new Foo();
switch (f) // Comment this line to compile
//switch ((En)f) // Uncomment this line to compile
{
case En.One:
break;
}
}
//////////////////////////////////////////////////////////////////
public enum En
{
One,
Two,
Three,
}
public class Foo
{
En _myEn;
public static implicit operator En(Foo f)
{
return f._myEn;
}
}
Run Code Online (Sandbox Code Playgroud)
从规范编辑:
switch语句的控制类型由switch表达式建立.如果switch表达式的类型是sbyte,byte,short,ushort,int,uint,long,ulong,char,string或enum-type,那么这就是switch语句的控制类型.否则,从switch表达式的类型到以下可能的控制类型之一,必须存在一个用户定义的隐式转换(第6.4节): …
我正在证明我的论文,我想知道如何描述我使用的语言
组合或其他什么?
首先,我运行我的应用程序,异常抛出任何错误(处理与否).
其次我使用a TypeConverter
从用户输入字符串转换为实际对象.
第三个TypeConverter
没有提供任何TryConvert
方法,所以我不得不使用异常进行验证,在这里使用这个相当丑陋的代码:
try
{
this._newValue = null;
#pragma Magic_SuppressBreakErrorThrown System.Exception
this._newValue = this.Converter.ConvertFromString(this._textBox.Text);
#pragma Magic_ResumeBreakErrorThrown System.Exception
this.HideInvalidNotification();
}
catch (Exception exception)
{
if (exception.InnerException is FormatException)
{
this.ShowInvalidNotification(this._textBox.Text);
}
else
{
throw;
}
}
Run Code Online (Sandbox Code Playgroud)
我发现它有相当分散注意力VS破执行的每次我输入-
的-1
,或者一些其他的无效字符.我可以使用与此类似的东西,但不是我转换为所有类型的TryParse
方法.
我希望有一些方法可以在try
不改变我的异常设置的情况下禁用中断代码段.
我正在尝试强制抽象类的继承者来初始化基类中的一个字段.
我无法通过基础中的构造函数执行此操作,因为字段的类型需要引用回创建它的对象.(我想我可以为字段类型提供一个无参数的ctor.但我觉得这只会改变问题,无论如何它应该是不可变的).
也许有人可以建议怎么做,或者可能做到这样我根本不需要这样做?
这就是我正在做的事情:
public abstract class Base
{
// Would like to force setting in CTOR
protected BaseImplementation _implementation;
protected BaseImplementation Implementation
{ get { return _implementation; } }
public void DoSomething()
{
Implementation.DoSomething();
}
}
public abstract class BaseImplementation
{
public abstract void DoSomething();
}
public class MyObject : Base
{
// Note its Nested
public class MyImplementation : BaseImplementation
{
private MyObject _myObject;
public MyImplementation(MyObject myObject)
{
this._myObject = myObject;
}
public override void DoSomething()
{
// …
Run Code Online (Sandbox Code Playgroud) 假设我们有一个函数MutateX
可以改变一个可变对象x
(在我的例子中是一个byte []),这不足为奇地改变了x
状态.我可以MutateX
用两种方式来定义
void MutateX (ref Object x); // (A)
// or
void MutateX (Object x); // (B)
Run Code Online (Sandbox Code Playgroud)
无论如何,X仍然是参考值 - 尽管如此.
对我来说ref
,该函数将以x
某种方式改变(无论它是否将实际参考值更改为某个其他对象).是否有一种形式优先于另一种形式?
我注意到在SerialPort.Read中没有实现
c# ×9
.net ×2
c#-4.0 ×1
enums ×1
generics ×1
inheritance ×1
performance ×1
postsharp ×1
pragma ×1
reflection ×1
swizzling ×1