1

i have a problem in creating an entity object. i dont know the object type, but i have the name of the entity to create and the name of its primary key. this function shows the problem:

        /// <summary>
    /// create a new entity and save it o object context
    /// </summary>
    /// <param name="oc"></param>
    /// <param name="entityName">type name of new entity</param>
    /// <param name="pkName">primary key name of new entity</param>
    /// <returns></returns>
    Guid genericCreate(ObjectContext oc, String entityName, String pkName)
    {
        //  create a new primary key
        Guid newPk = Guid.NewGuid();

        //  create new entity key
        EntityKey entityKey = new EntityKey(oc.GetType().Name + "." + entityName, pkName, newPk);

        //  get type name of new entity
        String typeName = oc.GetType().Namespace + "." + entityKey.EntitySetName;

        //  create a new entity


        //  this dont work :/
        //  Error: Operator '<' cannot be applied to operands of type 'method group' and 'System.Type'
        var entity = oc.CreateObject<System.Type.GetType(typeName)>();

        //  this works, but we run in problems at another place
        //  var anotherEntity = Activator.CreateInstance(System.Type.GetType(typeName));


        //  assign entity key
        IEntityWithKey entityWithKey = (IEntityWithKey)entity;
        entityWithKey.EntityKey = entityKey;

        //  add entity to object context
        String entitySetName = getEntitySetName(entityName);
        oc.AddObject(entitySetName, entityWithKey);

        //  write to database
        oc.SaveChanges();

        return newPk;
    }
coyote76
  • 13
  • 2
  • It doesn't work that way, because generic parameters are not `System.Type`s, but types which are known at compile-time. `GetType()` returns the type name at runtime. Eranga's answer is correct in that you have to use reflection to get the dynamic method handle. – Andreas Oct 13 '11 at 09:20
  • possible duplicate of [How to use reflection to call generic Method?](http://stackoverflow.com/questions/232535/how-to-use-reflection-to-call-generic-method) – nawfal Jan 18 '14 at 05:04

1 Answers1

1

You can use reflections to make the call to CreateObject method as described here.

Guid genericCreate(ObjectContext oc, String entityName, String pkName)
{
    //  create a new primary key
    Guid newPk = Guid.NewGuid();

    //  create new entity key
    EntityKey entityKey = new EntityKey(oc.GetType().Name + "." + entityName, pkName, newPk);

    //  get type name of new entity
    String typeName = oc.GetType().Namespace + "." + entityKey.EntitySetName;

    //  create a new entity

    var method = oc.GetType().GetMethod("CreateObject");
    method = method.MakeGenericMethod(System.Type.GetType(typeName));
    var entity = method.Invoke(service, null);


    //  assign entity key
    IEntityWithKey entityWithKey = (IEntityWithKey)entity;
    entityWithKey.EntityKey = entityKey;

    //  add entity to object context
    String entitySetName = getEntitySetName(entityName);
    oc.AddObject(entitySetName, entityWithKey);

    //  write to database
    oc.SaveChanges();

    return newPk;
}
Community
  • 1
  • 1
Eranga
  • 32,181
  • 5
  • 97
  • 96