开发者

C#, quick generics question

开发者 https://www.devze.com 2022-12-08 21:21 出处:网络
I have a need to create a quick class with just 2 properties (left and top), I\'ll then call these in a collection.

I have a need to create a quick class with just 2 properties (left and top), I'll then call these in a collection.

Is there a quick way to create the class 开发者_如何学Cstructure without having to actually create the strongly typed class itself using generics?

Thanks in advance

Better still, does the framwework have a built in type than can just store left, top, right, bottom co-ordinates in integer values?


Automatic Properties would help make this quick

public class Position<T> where T: struct
{
  public T Top { get; set; }
  public T Left { get; set; }
}

Or you might want to check out the Point or Rectangle classes in the System.Drawing namespace.


I think you're looking for System.Drawing.Rectangle (which is a struct, not a class by the way; there's a class in System.Windows.Shapes but that's different.) There's no point in creating a new generic type when what you want is already in the framework.


What's your reason for doing this? Why not just create the class?

If you really need to defer things, you can create an interface:

public interface IMyDeferredClass
{
    int MethodReturningInt(int parameter);
    int IntegerProperty { get; set; }
    int this[int index] { get; }
    event EventHandler SomeEvent;
}

You can program to IMyDefferedClass, but you'll eventually need a class to implement that interface:

public class MyDeferredClass : IMyDeferredClass
{
    public int MethodReturningInt(int parameter)
    {
        return 0;
    }

    public int IntegerProperty
    {
        get { return 0; }
        set {  }
    }

    public int this[int index]
    {
        get { return 0; }
    }

    public event EventHandler SomeEvent;
}


in C# 3.0 you would need to use reflection.

Both of these suggestions can have substantial performance overhead.

static void Main(string[] args)
{
    var obj = new { Name = "Matt" };
    var val = DoWork(obj); // val == "Matt"
}

static object DoWork(object input)
{
    /* 
       if you make another anonymous type that matches the structure above
       the compiler will reuse the generated class.  But you have no way to 
       cast between types.
    */
    var inputType = input.GetType();
    var pi = inputType.GetProperty("Name");
    var value = pi.GetValue(input, null);
    return value;
}

in C# 4.0 you could use the "dynamic" type

static object DoWork(dynamic input)
{
    return input.Name;
}

interesting Hack pointed out by Jon Skeet

static object DoWork(object input)
{
    var casted = input.Cast(new { Name = "" });
    return casted.Name;
}

public static class Tools
{
    public static T Cast<T>(this object target, T example)
    {
        return (T)target;
    }
}


No sorry. Anonymous classes can only be used in the same method without using some horible hack from Jon. (See comments)

0

精彩评论

暂无评论...
验证码 换一张
取 消