I'm trying to create a "smart" library that can store different objects in a DynamoDb table. For this, I built on the answer to this question in Stackoverflow to generate a new type in runtime according to the fields that we need to save in the table.
https://stackoverflow.com/a/3862241/2390800
The scenario is the following:
1- I create a runtime type. It works perfectly.
var myNewType = MyTypeBuilder.CreateNewType(
//
// the name for my runtime type
"my_type",
// A list with information about each field to save.
// The structure for each item is one class like this:
// class Field {
// public string Name { get; set; }
// public string Value { get; set; }
// }
// where:
// Name => name of the field in DynamoDb
// Value => value for the field in DynamoDb
fieldAndValueList
);
2- I instance the runtime type by using Activator. It works perfectly.
var myObjectToStore = Activator.CreateInstance(myNewType);
3- I call my "dynamoDbContext" by using reflection due to the "SaveAsync" method is generic and I don't know the "type" of the object to store. I get a Task because "SaveAsync" is async. It works perfectly.
var task = (Task) dynamoDbContext
.GetType()
.GetMethods()
.First(
m => m.Name == "SaveAsync" &&
m.IsGenericMethod &&
m.GetParameters().Any(p => p.ParameterType == typeof(DynamoDBOperationConfig))
)
.MakeGenericMethod(myNewType)
.Invoke(dynamoDbContext, new object[] { myObjectToStore, MyDynamoTableConfig, null });
4- Now, I wait for the task result.
task.Wait();
5- I get the following error:
System.InvalidOperationException: Type System.Object is unsupported, it has no supported members
at Amazon.DynamoDBv2.DataModel.StorageConfig..ctor(ITypeInfo targetTypeInfo)
at Amazon.DynamoDBv2.DataModel.ItemStorageConfig..ctor(ITypeInfo targetTypeInfo)
at Amazon.DynamoDBv2.DataModel.ItemStorageConfigCache.CreateStorageConfig(Type baseType, String actualTableName)
at Amazon.DynamoDBv2.DataModel.ItemStorageConfigCache.GetConfig(Type type, DynamoDBFlatConfig flatConfig, Boolean conversionOnly)
at Amazon.DynamoDBv2.DataModel.DynamoDBContext.SerializeToDocument(Object value, Type type, DynamoDBFlatConfig flatConfig)
at Amazon.DynamoDBv2.DataModel.DynamoDBContext.ToDynamoDBEntry(SimplePropertyStorage propertyStorage, Object value, DynamoDBFlatConfig flatConfig, Boolean canReturnScalarInsteadOfList)
at Amazon.DynamoDBv2.DataModel.DynamoDBContext.TryToList(Object value, Type type, DynamoDBFlatConfig flatConfig, DynamoDBList& output)
at Amazon.DynamoDBv2.DataModel.DynamoDBContext.ToDynamoDBEntry(SimplePropertyStorage propertyStorage, Object value, DynamoDBFlatConfig flatConfig, Boolean canReturnScalarInsteadOfList)
at Amazon.DynamoDBv2.DataModel.DynamoDBContext.PopulateItemStorage(Object toStore, ItemStorage storage, DynamoDBFlatConfig flatConfig, Boolean keysOnly, Boolean ignoreNullValues)
at Amazon.DynamoDBv2.DataModel.DynamoDBContext.ObjectToItemStorageHelper(Object toStore, ItemStorageConfig config, DynamoDBFlatConfig flatConfig, Boolean keysOnly, Boolean ignoreNullValues)
at Amazon.DynamoDBv2.DataModel.DynamoDBContext.ObjectToItemStorage(Object toStore, Type objectType, Boolean keysOnly, DynamoDBFlatConfig flatConfig)
at Amazon.DynamoDBv2.DataModel.DynamoDBContext.ObjectToItemStorage[T](T toStore, Boolean keysOnly, DynamoDBFlatConfig flatConfig)
at Amazon.DynamoDBv2.DataModel.DynamoDBContext.SaveHelperAsync[T](T value, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken)
Why does AWS say that I'm using "System.Object" if I'm specifying the "Type" by using reflection?