In a development that I am doing, I create a type of object from one of my classes that are stored in my library in the following way:
var type = GetTypeFromAssembly(typeName, fullNameSpaceType); var instanceOfMyType = Activator.CreateInstance(type); ReadObject(instanceOfMyType.GetType().GetProperties(), instanceOfMyType, fullNameSpaceType); return instanceOfMyType;
Then using Entity Framework I need to obtain by ID the values belonging to that object by instantiating in the aforementioned way and to be dynamic I do it in the following way:
var parameter = Expression.Parameter(typeof(TObject)); var condition = Expression.Lambda<Func<TObject, bool>>( Expression.Equal( Expression.Property(parameter, theEntity.GetType().GetProperty("Id").Name), Expression.Constant(id, typeof(TKey)) ), parameter ).Compile(); var theObject = _unitOfWork.Set<TObject>().Where(condition).FirstOrDefault();
The issue is that my TObject is of the type of the object that I dynamically instantiated when trying to execute the expression does not execute the process correctly and sends the following error:
Instance property 'Id' has not been defined for type 'System.Object'
Which is true since I'm sending the type object and I need to send the type of the instance of my object made in reflection, that is:
var theObject = _catalogService.CreateObjectInstance(catalog, "Cms.Spv.Entities"); _catalogService.GetObjectById<Guid, theObject>(id, theObject);
Any way to do this?