'object'在同一个程序集中不包含'...'的定义 [英] 'object' does not contain a definition for '...' within same assembly

查看:187
本文介绍了'object'在同一个程序集中不包含'...'的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我老板的建议,我们正在使用Dapper访问我们的数据库。由于这只是一个快速的一次性项目,他希望放弃创建POCO对象,以获得对数据库的这些调用的结果,所以我们只是将Dapper返回 dynamic 对象。

Per my boss' recommendations, we are using Dapper to access our database. As this is just a quick, throwaway project, he would like to forego creating POCO objects for the results of these calls to the database, so we're just having Dapper return dynamic objects.

所以我有几个Web API控制器,都来自同一BaseController。在基类中,我们有一个这样的方法:

So I have several Web API controllers, all deriving from the same BaseController. In the base class, we have a method like this:

protected dynamic ExecuteSingle(string sql, object parameters = null, bool useStoredProcedure = true) {
    using (var db = ObjectFactory.GetInstance<IDbManager>())
    {
        var cmd = useStoredProcedure ? db.SetSpCommand(sql) : db.SetCommand(sql);

        if (parameters != null)
        {
            cmd = cmd.SetParameters(parameters);
        }

        return cmd.ExecuteObject<dynamic>();
    }
}

我们可以在使用该结果时成功使用并将其作为参数传递给 Ok()返回值。对象的所有属性都成功解析成JSON对象。

We are able to use that successfully when taking that result and passing it as the parameter to a Ok() return value. All of the properties of the object get successfully parsed into the JSON object.

我现在正在处理另一部分代码,而不是立即将其重新分发出来,需要使用数据回到另一组功能。

I'm now working on another section of code where instead of immediately spitting it back out, I need to use the data coming back to hit another set of functionality.

var sql = " SELECT Id FROM Table WHERE ReviewId = :ReviewId ";
dynamic dbSurvey = ExecuteSingle(sql, new {ReviewId = reviewId}, false);

var survey = sg.GetSurvey(new GetSurveyRequest(dbSurvey.Id));

然后在最后一行失败,调用 dbSurvey.Id 有一个例外说

It then fails on that last line where calling dbSurvey.Id with an exception saying


'object'不包含'Id'的定义

'object' does not contain a definition for 'Id'

检查对象,有一个名为Id的属性。

Checking the object, there is a property on there named "Id".

我有一个已检查 几个 questions ,即使它们都是处理匿名对象(我可以理解为 dynamic 标题),我正在同一个大会上,所以关于匿名对象被宣布为内部的点将不适用于这种情况。

I have checked several questions already, and even though they are all dealing with Anonymous objects (which I could understand dynamic being under that heading), I am staying within the same assembly, so those points about Anonymous objects being declared as "internal" wouldn't apply in this case.

我也tr将 ExecuteSingle 的返回类型更改为 ExpandoObject ,但获得的结果相同。

I also tried changing the return type of ExecuteSingle to an ExpandoObject, but am getting the same results.

编辑
这是一个关于它被调用的屏幕截图,而不是包含它的对象和属性。也许属性的格式可能有助于确定为什么没有被发现。

EDIT Here is a screenshot of how it is being called, versus the object and properties that it contains. Perhaps the format of the properties could help determine why it's not being found.

我还使用Watch菜单来尝试访问该属性的各种方法,而且以下任何一种都不起作用:

I also used the Watch menu to try various ways to access the property, and none of the following worked:

dbSurvey["Id"]
(dbSurvey as IDictionary<string, long>)["Id"]
((IDictionary<string, int>)dbSurvey)["Id"]

运行时直接窗口中的 dbSurvey.GetType()的结果:

Here is the result of dbSurvey.GetType() from the immediate window while running:

{<>f__AnonymousType2`1[System.Int64]}
base: {Name = "<>f__AnonymousType2`1" FullName = "<>f__AnonymousType2`1[[System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"}
Assembly: {***.Web.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null}
AssemblyQualifiedName: "<>f__AnonymousType2`1[[System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], ***.Web.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
BaseType: {Name = "Object" FullName = "System.Object"}
ContainsGenericParameters: false
DeclaringMethod: 'dbSurvey.GetType().DeclaringMethod' threw an exception of type 'System.InvalidOperationException'
DeclaringType: null
FullName: "<>f__AnonymousType2`1[[System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"
GenericParameterAttributes: 'dbSurvey.GetType().GenericParameterAttributes' threw an exception of type 'System.InvalidOperationException'
GenericParameterPosition: 'dbSurvey.GetType().GenericParameterPosition' threw an exception of type 'System.InvalidOperationException'
GUID: {382c0269-d631-3c89-a105-38a1be8a3db7}
IsConstructedGenericType: true
IsEnum: false
IsGenericParameter: false
IsGenericType: true
IsGenericTypeDefinition: false
IsSecurityCritical: true
IsSecuritySafeCritical: false
IsSecurityTransparent: false
MemberType: TypeInfo
MetadataToken: 33554480
Module: {***.Web.Test.dll}
Name: "<>f__AnonymousType2`1"
Namespace: null
ReflectedType: null
StructLayoutAttribute: {System.Runtime.InteropServices.StructLayoutAttribute}
TypeHandle: {System.RuntimeTypeHandle}
UnderlyingSystemType: {Name = "<>f__AnonymousType2`1" FullName = "<>f__AnonymousType2`1[[System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"}


推荐答案

好的,所以我解决了这个问题。我只是想出了什么问题。感谢xanatos在评论中的帮助,我们注意到,该类型的模块属性来自测试工具。那当时对我来说没有任何意义,因为这个动态对象是在同一个程序集中创建的。

OK, so I resolved this issue. I just figured out what the problem was. Thanks to xanatos' help in the comments, we noticed that the Module property of the type was coming from the Test harness. That didn't make any sense to me at the time, because the dynamic object was being created in the same assembly.

然而,我没有想到,直到我今天早上回到这个问题是,在基础API控制器中创建对象的源是我在测试工具中创建的一个匿名对象。所以 Module 是正确的。

However, what I didn't think about until I came back to the issue this morning was that the source of the object being "created" in the base API Controller was an anonymous object that I was creating in the Test harness. So that Module was correct.

如果我在测试中为Mock对象创建了一个单独的POCO对象DLL,那么它不再在另一个程序集中创建的匿名对象中创建一个动态

If I went and created a separate POCO object for the Mock object in the Test DLL, then it was no longer creating a dynamic off of an anonymous object created in another assembly.

这篇关于'object'在同一个程序集中不包含'...'的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆