JL.
JL.

Reputation: 81262

C#, quick generics question

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 structure 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?

Upvotes: 4

Views: 212

Answers (5)

Geoff
Geoff

Reputation: 4746

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

Upvotes: 1

Matthew Whited
Matthew Whited

Reputation: 22433

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;
    }
}

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1499770

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.

Upvotes: 6

NerdFury
NerdFury

Reputation: 19214

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.

Upvotes: 9

John Saunders
John Saunders

Reputation: 161773

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;
}

Upvotes: 2

Related Questions