首页 \ 问答 \ 将列表从一种类型转换为另一种类型(Cast list from one type to another)

将列表从一种类型转换为另一种类型(Cast list from one type to another)

我在将列表传递给接受不同类型参数的方法时遇到问题。

我的命名空间中有以下方法:Common.DB2(One classlibrary)

public List<MyModel> GetDB2Data()
{
 //Get data from db2
}

MyModel属于Common.Db2名称空间

我的命名空间Common.Sql(另一个类库)中有另一种方法

public void PostToSql(List<MyModel> listData)
{
 //Post data to sql
}

MyModel属于Common.Sql名称空间的位置

所以基本上我从db2获取一些数据并将其发布到sql。

在另一个类库中,我获取此数据并将数据发布为:

//Get Data
var data =  db2Repo.GetDB2Data();

//Post Data 
sqlRepo.PostToSql(data);

在上面的行中它没有说明

Argument 1: cannot convert from 
  'System.Collections.Generic.List<Common.DB2.MyModel>' to 
  'System.Collections.Generic.List<Common.Sql.MyModel>'

我曾尝试过像下面这样的演员:

List<Common.Sql.MyModel> sourceList = new List<Common.Sql.MyModel>();
sourceList =  data.Cast<Common.Sql.MyModel>().ToList();

但上面再次抛出异常

Unable to cast object of type 'Common.DB2.MyModel' to type 
'Common.Sql.MyModel'.

有任何更好的方法来处理/设计这个。


I am having issues in passing list to a method that accepts parameter of different type.

I have the following method in my namespace: Common.DB2 (One classlibrary)

public List<MyModel> GetDB2Data()
{
 //Get data from db2
}

Where MyModel belongs to Common.Db2 namespace

I have another method in my namespace Common.Sql (Another Classlibrary)

public void PostToSql(List<MyModel> listData)
{
 //Post data to sql
}

Where MyModel belongs to Common.Sql namespace

So basically I get some data from my db2 and post it to sql.

In another class library I get this data and post data as:

//Get Data
var data =  db2Repo.GetDB2Data();

//Post Data 
sqlRepo.PostToSql(data);

Here in the above line it fails stating

Argument 1: cannot convert from 
  'System.Collections.Generic.List<Common.DB2.MyModel>' to 
  'System.Collections.Generic.List<Common.Sql.MyModel>'

I had tried casting like below:

List<Common.Sql.MyModel> sourceList = new List<Common.Sql.MyModel>();
sourceList =  data.Cast<Common.Sql.MyModel>().ToList();

But above again throws exception

Unable to cast object of type 'Common.DB2.MyModel' to type 
'Common.Sql.MyModel'.

Any better way to handle/design this.


原文:https://stackoverflow.com/questions/46490960
更新时间:2022-03-21 20:03

最满意答案

如果您需要使用div,您可以这样做,但我建议您使用按钮或输入类型提交。

<form id="form-id" method="post" action="your-php-file-url">
<input type="hidden" name="your-variable-name" value="your-variable-value">
<div onclick="document.getElementById('form-id').submit();">Click Me</div>
</form>

您也可以使用jQuery或其他一些JS库。

注意:请记住,如果您发送的数据是通过浏览器提供的,那么操作起来非常容易(如果您使用POST或GET则不会处理),因此在处理时检查它是很重要的。


If you need to use div you can do it like this but I suggest that you use button or input of type submit.

<form id="form-id" method="post" action="your-php-file-url">
<input type="hidden" name="your-variable-name" value="your-variable-value">
<div onclick="document.getElementById('form-id').submit();">Click Me</div>
</form>

Also you may use jQuery or some other JS library.

NOTE: Keep in mind that if the data that you send is provided via browser it's really easy to manipulate (doesn't mater if you use POST or GET) so it's important to check it out when you process it.

相关问答

更多
  • 如果您需要使用div,您可以这样做,但我建议您使用按钮或输入类型提交。
    Click Me
    ...
  • 你可以有一个显示可以是'无'的按钮。 在客户端处理点击Div,然后从那里点击该按钮的Click并处理按钮的Click事件中的Server Side。