如何从方法参数自动生成 poco 类(对象)? [英] How to auto generate poco class (object) from a method parameters?

查看:18
本文介绍了如何从方法参数自动生成 poco 类(对象)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么简单的方法可以从带有参数的现有方法中自动生成 poco 类?

Is there any short way of auto generating poco class from an existing method with parameters?

 public void RegisterUser(string userName, string fullName, 
string tel, string mobile, string website, string address, string blah){
    //--
    }

从这个方法我应该能够自动生成我的类,如下所示:

from this method I should be able to auto generate my class as below:

class UserRegisterDetail{

public string UserName {get;set;}
public string FullName {get;set;}
public string Tel {get;set;}
public string Mobile {get;set;}
public string Website {get;set;}
public string Address {get;set;}
public string Blah {get;set;}
}

我使用 resharper 和重构,但我无法生成简单的 poco 类.

I use resharper and re-factoring but I cannot generate the simple poco class.

更新:

使用从参数中提取类"选项生成以下内容:

Using "Extract class from parameters" option generates the following:

    public class RegisterUserParams
        {
            private string userName;
            private string email;
            private string fullName;
            private string jobTitle;
            private string department;
            private string tel;
            private string mobile;
            private string switchboard;
            private string fax;
            private string address1;
            private string address2;
            private string address3;

            public RegisterUserParams(string userName, string email, string fullName, string jobTitle, string department, string tel, string mobile, string switchboard, string fax, string address1, string address2, string address3 )
            {
                this.userName = userName;
                this.email = email;
                this.fullName = fullName;
                this.jobTitle = jobTitle;
                this.department = department;
                this.tel = tel;
                this.mobile = mobile;
                this.switchboard = switchboard;
                this.fax = fax;
                this.address1 = address1;
                this.address2 = address2;
                this.address3 = address3;

            }
public string UserName
            {
                get { return userName; }
            }

            public string Email
            {
                get { return email; }
            }

            public string FullName
            {
                get { return fullName; }
            }

            public string JobTitle
            {
                get { return jobTitle; }
            }

            public string Department
            {
                get { return department; }
            }

            public string Tel
            {
                get { return tel; }
            }

            public string Mobile
            {
                get { return mobile; }
            }

            public string Switchboard
            {
                get { return switchboard; }
            }

            public string Fax
            {
                get { return fax; }
            }

            public string Address1
            {
                get { return address1; }
            }

            public string Address2
            {
                get { return address2; }
            }

            public string Address3
            {
                get { return address3; }
            }         
        }

推荐答案

在我看来,您应该只创建具有您想要的属性作为参数的类并传递它的一个实例作为参数.比为方法设置这么多参数要好.

It would seem to me that you should just create the class with the properties you want as parameters and pass an instance of it as the parameter. Better than having so many parameters for the method.

例如:

public void RegisterUser(UserRegisterDetail pInstanceOfMyClass)
{ ... }

UserRegisterDetail 是您要传递的那些属性的类.

Where UserRegisterDetail is you class with those properties you want to pass.

这篇关于如何从方法参数自动生成 poco 类(对象)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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