System.Reflection.TargetException未被用户代码处理 - 非静态方法需要一个目标?

时间:2014-06-24 09:17:03

标签: c# asp.net-mvc runtime-error orchardcms

感谢您的观看: 我在我的一个服务类中收到以下错误,我完全不知道它需要什么/为什么我收到它[我刚刚开始编程]:

  
    

System.Reflection.TargetException未被用户代码处理HResult = -2146232829消息=非静态方法需要目标。
    Source = mscorlib StackTrace:            at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)            在System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj,     BindingFlags invokeAttr,Binder binder,Object []参数,     文化信息文化)            在System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object []参数,     文化信息文化)            在System.Reflection.RuntimePropertyInfo.SetValue(Object obj,Object value,BindingFlags invokeAttr,Binder binder,Object [] index,     文化信息文化)            在System.Reflection.RuntimePropertyInfo.SetValue(Object obj,Object value,Object [] index)            在Orchard.ContentManagement.InfosetHelper.Store [TPart,TRecord,TProperty](TPart     contentPart,Expression 1 targetExpression, TProperty value) in e:\Projects\_Code\_ORCHARD\orchard - Copy\src\Orchard\ContentManagement\InfosetHelper.cs:line 122 at Orchard.ContentManagement.ContentPart 1.Store [TProperty](表达式1 targetExpression, TProperty value) in e:\Projects\_Code\_ORCHARD\orchard - Copy\src\Orchard\ContentManagement\ContentPart.cs:line 130 at EA.Profile.Models.AddressPart.set_Street1(String value) in e:\Projects\_Code\_ORCHARD\orchard - Copy\src\Orchard.Web\Modules\EA.Profile\Models\AddressPart.cs:line 14 at EA.Profile.Services.AddressService.CreateAddress(AddressPartRecord addressdetails) in e:\Projects\_Code\_ORCHARD\orchard - Copy\src\Orchard.Web\Modules\EA.Profile\Services\AddressService.cs:line 27 at EA.Profile.Controllers.AddressController.Create(AddressPartRecord addressdetails) in e:\Projects\_Code\_ORCHARD\orchard - Copy\src\Orchard.Web\Modules\EA.Profile\Controllers\AddressController.cs:line 50 at lambda_method(Closure , ControllerBase , Object[] ) at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary 2个参数)            在System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext     controllerContext,ActionDescriptor actionDescriptor,IDictionary 2 parameters) at System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod() at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult 2.CallEndDelegate(IAsyncResult)     asyncResult)            在System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()            在System.Web.Mvc.Async.AsyncResultWrapper.End [TResult](IAsyncResult)     asyncResult,Object标签)            在System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult)     asyncResult)            在System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3f()            在System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters。&lt;&gt; c__DisplayClass48.b__41()     的InnerException:

  

有问题的服务如下:

using EA.Profile.Models;
using Orchard;
using Orchard.ContentManagement;

namespace EA.Profile.Services
{
    public interface IAddressService : IDependency {
        AddressPart CreateAddress(AddressPartRecord addressdetails);
    }

    public class AddressService : IAddressService {
        private readonly IOrchardServices _orchardServices;

        public AddressService(IOrchardServices orchardServices) {
            _orchardServices = orchardServices;
        }

        public AddressPart CreateAddress(AddressPartRecord addressdetails) {
            var address = _orchardServices.ContentManager.New("Address");
            //var profilePart = address.As<ProfilePart>();
            var addressPart = address.As<AddressPart>();

            addressPart.Street1 = addressdetails.Street1; <error>
            addressPart.Street2 = addressdetails.Street2;
            addressPart.City = addressdetails.City;
            addressPart.State = addressdetails.State;
            addressPart.PostCode = addressdetails.PostCode;
            addressPart.Country = addressdetails.Country;

            _orchardServices.ContentManager.Create(address);

            return addressPart;
        }
    }
}

...如果它有所作为,我正在努力学习Orchard CMS [到目前为止,Orchard论坛在支持方面并不是很有成效],这段代码来自我用所有创建的自定义模块适当的处理程序/驱动程序/模型/迁移等。最初我虽然我的Migrations.cs可能有问题:

namespace EA.Profile
{
    public class Migrations : DataMigrationImpl {

        private readonly IOrchardServices _orchardServices;

        public Migrations(IOrchardServices orchardServices)
        {
            _orchardServices = orchardServices;
        }
        public int Create() {

            //Profile
            SchemaBuilder.CreateTable("ProfilePartRecord",
                table => table
                    .ContentPartRecord()
                    //ProfilePartRecord_id [Auto Key]
                    .Column<string>("FirstName")
                    .Column<string>("LastName")
                    //System
                    .Column<DateTime>("CreatedAt")
                );

            //Address
            SchemaBuilder.CreateTable("AddressPartRecord",
                table => table
                    .ContentPartRecord()
                    .Column<int>("ProfilePartRecord_id") //FK
                    .Column<string>("Street1")
                    .Column<string>("Street2")
                    .Column<string>("City")
                    .Column<string>("State")
                    .Column<short>("PostCode")
                    .Column<string>("Country")
                );

            //Address Type
            SchemaBuilder.CreateTable("AddressTypePartRecord",
                table => table
                    .ContentPartRecord()
                    .Column<int>("AddressPartRecord_id") //FK
                    .Column<string>("Type")
                );

            ContentDefinitionManager.AlterPartDefinition("ProfilePart",
                builder => builder.Attachable());

            ContentDefinitionManager.AlterTypeDefinition("Profile", t => t
                .WithPart(typeof(ProfilePart).Name)
                .WithPart("UserPart")
                );

            ContentDefinitionManager.AlterTypeDefinition("User", t => t
                .WithPart("ProfilePart")
                );


            // ORCHARD Settings: I believe the positioning of these settings is critical. For I had them at the start of this
            // method, and although the checkboxes in admin were indeed changed, they were not saved; I had to uncheck [save], recheck [save]
            // inorder for the settings to trigger/have any effect.
            var registrationSettings = _orchardServices.WorkContext.CurrentSite.As<RegistrationSettingsPart>();
            registrationSettings.UsersCanRegister = true;
            registrationSettings.UsersAreModerated = true;

            return 1;
        }

        public int UpdateFrom1()
        {
            ContentDefinitionManager.AlterPartDefinition("AddressPart",
                builder => builder.Attachable());

            ContentDefinitionManager.AlterTypeDefinition("Address", t => t
                .WithPart(typeof(AddressPart).Name)
                .WithPart("ProfilePart")
                );

            ContentDefinitionManager.AlterTypeDefinition("Profile", t => t
                .WithPart("AddressPart")
                );

            return 2;
        }
    }
} 

...但是在检查数据库时,所有表都在那里并正常运行。

调试器中断的地方是(这里):

public static void Store<TPart, TRecord, TProperty>(this TPart contentPart,
    Expression<Func<TRecord, TProperty>> targetExpression,
    TProperty value)
    where TPart : ContentPart<TRecord> {

    var propertyInfo = ReflectionHelper<TRecord>.GetPropertyInfo(targetExpression);
    var name = propertyInfo.Name;
    var versioned = typeof(ContentPartVersionRecord).IsAssignableFrom(typeof(TRecord));
    propertyInfo.SetValue(contentPart.Record, value, null); //<Here>
    contentPart.Store(name, value, versioned);
}

由于某种原因,按照:

Non-static method requires a target C#

contentPart.Record [期待成为AddressPart]不属于AddressPart,我无法理解为什么那是为了我的生命?我完全盲目[没有经验]看到明显的,或者有人希望能让我摆脱痛苦。谢谢你的支持,罗恩

//AddressPartRecord.cs

using System.ComponentModel.DataAnnotations;
using Orchard.ContentManagement.Records;

namespace EA.Profile.Models
{
    public class AddressPartRecord : ContentPartRecord
    {
        public virtual int ProfilePartRecord_id { get; set; }
        [Required(ErrorMessage = "Street number and address required.")]
        public virtual string Street1 { get; set; }
        public virtual string Street2 { get; set; }
        [Required(ErrorMessage = "City required.")]
        public virtual string City { get; set; }
        [Required(ErrorMessage = "State required.")]
        public virtual string State { get; set; }
        [Required(ErrorMessage = "Post Code required.")]
        public virtual short PostCode { get; set; }
        [Required(ErrorMessage = "Country required.")]
        public virtual string Country { get; set; }
    }
}

//AddressPart.cs

using Orchard.ContentManagement;

namespace EA.Profile.Models
{
    public class AddressPart : ContentPart<AddressPart> {

        public int ProfilePartRecord_id
        {
            get { return Retrieve(x => x.ProfilePartRecord_id); }
            set { Store(x => x.ProfilePartRecord_id, value); }
        }
        public string Street1
        {
            get { return Retrieve(x => x.Street1); }
            set { Store(x => x.Street1, value); }
        }

        public string Street2
        {
            get { return Retrieve(x => x.Street2); }
            set { Store(x => x.Street2, value); }
        }

        public string City
        {
            get { return Retrieve(x => x.City); }
            set { Store(x => x.City, value); }
        }

        public string State
        {
            get { return Retrieve(x => x.State); }
            set { Store(x => x.State, value); }
        }

        public short PostCode
        {
            get { return Retrieve(x => x.PostCode); }
            set { Store(x => x.PostCode, value); }
        }

        public string Country
        {
            get { return Retrieve(x => x.Country); }
            set { Store(x => x.Country, value); }
        }

        public ProfilePart Profile
        {
            get { return this.As<ProfilePart>(); }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

据我所知,错误在这里:

public class AddressPart : ContentPart<AddressPart> { ... }

应该是

public class AddressPart : ContentPart<AddressPartRecord> { ... }

备份记录是一个单独的类,需要包含您在迁移中创建的表的所有属性。好像你错过了那一件事。 docs中有一个很好的例子。