Waldemar Gałęzinowski
Waldemar Gałęzinowski

Reputation: 1233

How can I create new object based on another one?

I have some not useful object with, let's say, 30 properties. Half of them are useful for me, so I want to create new useful object with only properties I need so the rest doesn't take space in object visualizers. I don't want to define new class and type them down. I want something like

var list = new List<SomeType> { usefulProp1, usefulProp2, ... };
var usefulObject = new NewItem(notUsefulObject, list);

where SomeType is not string (list doesn't contain property names).

Upvotes: 1

Views: 3649

Answers (3)

Waldemar Gałęzinowski
Waldemar Gałęzinowski

Reputation: 1233

var list = new List<Expression<Func<object>>> { () => notUsefulObject.usefulProp1, () => notUsefulObject.usefulProp2... };
var nm = new AssemblyName("MyDynamicAssembly");
TypeBuilder tb = Thread.GetDomain().DefineDynamicAssembly(nm, AssemblyBuilderAccess.RunAndSave).DefineDynamicModule(nm.Name, nm.Name + ".dll").DefineType("NewItem", TypeAttributes.Public);
const MethodAttributes GetSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
foreach (Expression b in list.Select(x => x.Body))
{
    MemberInfo mi = ((MemberExpression)b).Member;
    Type t = b.Type;
    FieldBuilder fb = tb.DefineField(mi.Name.ToLower(), t, FieldAttributes.Private);
    PropertyBuilder pb = tb.DefineProperty(mi.Name, PropertyAttributes.HasDefault, t, null);
    MethodBuilder getBld = tb.DefineMethod("get_" + mi.Name, GetSetAttr, t, Type.EmptyTypes);
    ILGenerator getGen = getBld.GetILGenerator();
    getGen.Emit(OpCodes.Ldarg_0);
    getGen.Emit(OpCodes.Ldfld, fb);
    getGen.Emit(OpCodes.Ret);
    MethodBuilder setBld = tb.DefineMethod("set_" + mi.Name, GetSetAttr, null, new[] { t });
    ILGenerator setGen = setBld.GetILGenerator();
    setGen.Emit(OpCodes.Ldarg_0);
    setGen.Emit(OpCodes.Ldarg_1);
    setGen.Emit(OpCodes.Stfld, fb);
    setGen.Emit(OpCodes.Ret);
    pb.SetGetMethod(getBld);
    pb.SetSetMethod(setBld);
}
object usefulObject = Activator.CreateInstance(tb.CreateType());

Upvotes: 2

Scott Chamberlain
Scott Chamberlain

Reputation: 127543

Because you said this problem was specific to object visualizers, the best solution is to make a Debugger Type Proxy for the class which can be used as a proxy.

[DebuggerTypeProxy(typeof(SomeTypeDebugView))]
public class SomeType
{
    public string Foo { get; set; }
    public string Bar { get; set; }

    internal class SomeTypeDebugView
    {
        private SomeType _someType;

        public SomeTypeDebugView(SomeType someType)
        {
            _someType = someType;
        }

        public string Foo { get { return _someType.Foo; } }
    }
}

class Program
{
    static void Main()
    {
        var someType = new SomeType();
        someType.Foo = "foo";
        someType.Bar = "bar";

        Debugger.Break();
    }
}

This will cause Bar to be hidden in the debug visualizer by default unless you choose to view the "Raw Type"

enter image description here

If you are looking for a more one time debugging type you can use annonamous types to create the new type you need in the watch window, for example his is new {Foo = someType.Foo, Bar = someType.Bar }

enter image description here

This also could be used with LINQ to perform the select over a IEnumerable, here for example is someTypeList.Select(x=> new {Foo = x.Foo, Bar = x.Bar})

enter image description here

Upvotes: 0

Kelso Sharp
Kelso Sharp

Reputation: 972

if its something permanent then doing it properly would be to create a class that inherits from your base class and populate only the properties that you need.

public UsefulObject : NotUsefulObject 
{
    public int MyProperty 
    {
       get
       {
          return base.MyProperty; // this is arbitrary you can do it however you want.
       }
       set
       {
          MyProperty = value;
       }
    }
}

Then you can use your reuse your useful object however you want.

var list = new List<UsefulObject>();

Upvotes: 1

Related Questions