首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Azure Table SDK2 EntityResolver使用反射

Azure Table SDK2 EntityResolver使用反射
EN

Stack Overflow用户
提问于 2013-03-15 15:38:02
回答 3查看 1.4K关注 0票数 0

我正在尝试使用EntityResolver从Azure TableResult.Execute中动态填充一个空对象,除非省略match.SetValue调用,否则调试不会进入Test方法。它抛出一个异常,说明下面的内容。实际的azure表请求很好,在debug中我可以看到列值等,我只需要将它映射到一个本地类中,最好使用泛型和反射。

代码语言:javascript
运行
复制
Method not found: 'System.Nullable`1<Int32> Microsoft.WindowsAzure.Storage.Table.EntityProperty.get_Int32Value()'.

我认为问题与反射有关,但需要帮助。

代码语言:javascript
运行
复制
public T RetrieveRow(string partitionKey, string rowKey)
    {
        EntityResolver<IObTable> resolver = (pk, rk, ts, props, etag) => Test(props);
        CloudTable table = base.TableClient.GetTableReference(TableName);
        TableOperation operation = TableOperation.Retrieve<IObTable>(partitionKey, rowKey, resolver);
        TableResult retrievedResult = table.Execute(operation);
        return (T)retrievedResult.Result;
    }

public IObTable Test(IDictionary<string, EntityProperty> storageProps)
    {
        IObTable objectToReturn = (IObTable)Activator.CreateInstance(typeof(T));
        if (storageProps != null)
        {
            var emptyObjectProps = objectToReturn.GetType().GetProperties();
            foreach (var prop in storageProps)
            {
                PropertyInfo match = emptyObjectProps.FirstOrDefault(v=> v.Name==prop.Key);
                if (match!=null)
                {
                    if (match.PropertyType == typeof(Int32))
                    {
                        match.SetValue(prop, storageProps[match.Name].Int32Value);
                    }
                }
            }
        }
        return objectToReturn;
    }

IObTable只是我本地实体上的一个标记接口。

任何帮助都非常感谢。

EN

回答 3

Stack Overflow用户

发布于 2013-05-28 02:35:11

改变这个: match.SetValue(prop,storagePropsmatch.Name.Int32Value);

对此: match.SetValue(objectToReturn,storagePropsmatch.Name.Int32Value);

票数 1
EN

Stack Overflow用户

发布于 2013-08-20 06:23:43

这是一个相当长的方法,但它是有效的:

代码语言:javascript
运行
复制
    //TEntity is any class derived from TableEnitity 
    public IEnumerable<TEntity> TestingGetPartitionEntities(string PartitionKey)
    {

        TableQuery<DynamicTableEntity> query = new TableQuery<DynamicTableEntity>().Where(
              TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, PartitionKey));

            EntityResolver<TEntity> resolver = (pk, rk, ts, props, etag) => 
            {
                //MUST create the 'reflected' instance in the 'EntityResolver', so resolver can create new instance foreach entity in query execution loop.
                TEntity objectToReturn = (TEntity)Activator.CreateInstance(typeof(TEntity));
                var objectProperties = objectToReturn.GetType().GetProperties();

                foreach (var prop in props)
                {
                    PropertyInfo match = objectProperties.FirstOrDefault(v => v.Name == prop.Key);
                    if (match != null)
                    {
                    if (match.PropertyType == typeof(Int32))
                    {
                        match.SetValue(objectToReturn, props[match.Name].Int32Value);
                    }
                    if (match.PropertyType == typeof(object))
                    {
                        match.SetValue(objectToReturn, props[match.Name].PropertyAsObject);
                    }
                    if (match.PropertyType == typeof(bool))
                    {
                        match.SetValue(objectToReturn, props[match.Name].BooleanValue);
                    }
                    if (match.PropertyType == typeof(byte[]))
                    {
                        match.SetValue(objectToReturn, props[match.Name].BinaryValue);
                    }
                    if (match.PropertyType == typeof(DateTimeOffset))
                    {
                        match.SetValue(objectToReturn, props[match.Name].DateTimeOffsetValue);
                    }
                    if (match.PropertyType == typeof(double))
                    {
                        match.SetValue(objectToReturn, props[match.Name].DoubleValue);
                    }
                    if (match.PropertyType == typeof(Guid))
                    {
                        match.SetValue(objectToReturn, props[match.Name].GuidValue);
                    }
                    if (match.PropertyType == typeof(int))
                    {
                        match.SetValue(objectToReturn, props[match.Name].Int32Value);
                    }
                    if (match.PropertyType == typeof(long))
                    {
                        match.SetValue(objectToReturn, props[match.Name].Int64Value);
                    }
                    if (match.PropertyType == typeof(string))
                    {
                        match.SetValue(objectToReturn, props[match.Name].StringValue);
                    }
                }
            }
            return objectToReturn as TEntity;
        };

        //This is the list that is returned to caller.
        List<TEntity> results = new List<TEntity>();
        foreach (TEntity entity in table.ExecuteQuery(query, resolver, null, null))
        {
            results.Add(entity as TEntity);
        }
        return results;
    }
票数 1
EN

Stack Overflow用户

发布于 2014-11-16 17:00:56

在Azure Storage组装本身(Microsoft.WindowsAzure.Storage.dll)中有一个EntityResolver代表的实现,称为EntityUtility.ResolveEntityByType<T>,您可以根据自己的需要对其进行调整。那个类是内部的,所以你必须反汇编dll,但有很多工具可以做到这一点(Reflector很棒,ILSpy是免费的)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15427049

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档