jQuery ajax를 사용하여 ASP.Net 웹 서비스 (ASMX 파일)를 호출하여 웹 페이지 데이터를 업데이트합니다.일반 형식 정의를 asmx webservice에 전달하는 방법
내가 도메인 객체 클래스 코드에서 데이터베이스 기억 별도의 코드가 (아래 예 코드를 동물 클래스 및 AnimalRepository 클래스가있다). 나는 모든 유사한 도메인 객체 저장소 클래스가 Store 메소드를 가질 것이라는 규칙을 구현 중이다.
나는이 저장소 클래스 중 하나의 저장 방법을 호출하기위한 수용 할 수있는 웹 서비스를 생성하고 싶습니다. 이를 작동 시키려면 아래 코드 샘플에서 List of Of Animal 매개 변수가 제네릭 형식이어야합니다.
나는 (객체) 목록을 시도했지만 형식을 변환 할 수 없습니다에 저장 명령 오류. 런타임까지 알 수 없기 때문에 List (Of Object)를 올바른 유형으로 캐스팅 할 수 있다고는 생각하지 않습니다.<WebMethod(True)> _
Public Sub StoreAnimals(ByVal _animals As List(Of Animal), ByVal _type As String)
Dim classType As Type = Type.GetType(_type & "Repository, MyCompany.Assembly")
Dim instanceOfClass = Activator.CreateInstance(classType)
Dim method As MethodInfo = classType.GetMethod("Store")
method.Invoke(instanceOfClass, New Object() {_animals})
End Sub
이상적으로, 내가 좋아하는 것 다음 웹 서비스 메소드 정의에서 제네릭의 여지가 없다,하지만 같은 최종 결과를 달성 할 수있는 트릭이
<WebMethod(True)> _
Public Sub StoreData(Of T)(ByVal _data As List(Of T), ByVal _type As String)
Dim classType As Type = Type.GetType(_type & "Repository, MyCompany.Assembly")
Dim instanceOfClass = Activator.CreateInstance(classType)
Dim method As MethodInfo = classType.GetMethod("Store")
method.Invoke(instanceOfClass, New Object() {_data})
End Sub
. 그렇지 않으면 첫 번째 매개 변수에서만 다른 많은 Store * 웹 서비스를 만들 예정입니다.
대단한 직업 - 적어도 나는 죽은 말을 때리는 데 더 이상 시간을 낭비하지 않을 것입니다. –