How do i assigned the type of any object to a .NET generic method?
문제
Suppose that I have created a method like this
private void Test<t>(t str)
{
}
Now from another method i call it
private void BtnClick()
{
string a = "";
test<Here I Want To assign The Type what ever the type of 'a' is>();
}
How can I do this ?
올바른 솔루션이 없습니다
다른 팁
Well, if a
is a string, you can write:
Test<string>(a);
And if a
is an int
, you can write:
Test<int>(a);
Pretty obvious. If you don't know the type of a
upfront, that means that BtnClick
should be generic, the problem would be moved to BtnClick
's caller:
private void BtnClick<T>()
{
T a = ...;
Test<T>(a);
}
C# 2.0 and later offers type inference, so you wouldn't have to specify the type:
Test(a);
You simply call the function passing a
as the parameter. Type inference will determine what T should be for a
.
static void Main()
{
string temp = "blah";
Test(temp);
Console.Read();
}
private static void Test<T>(T input)
{
Console.WriteLine(typeof(T).ToString());
}
System.String
would be printed to the console.
Edit: But, yes, absent type inference, and when you know what your type your variable is, you can always be explicit with the type. Test<string>(temp);
다른 옵션은 타이머 작업에서 클리닝 논리를 코딩하는 것입니다.
일일 / 주 / 월간 (야간 시간)을 실행하는 타이머 작업을 작성하고 작업 목록을 루프하고 취소 된 워크 플로우의 흑인 작업 및 작업을 삭제합니다.
1) 고아 작업 삭제
워크 플로에 의해 생성 된 작업 목록에서 시작된 목록 항목의 ID가 포함 된 목록 및 필드 'WorkFlowItemId'의 GUID를 포함하는 'workflowlistid'필드가 있습니다.
목록 워크 플로우에 의해 생성 된 작업은 'workflowlistid'및 '워크 플로이 입력'에서 null이 있습니다.
이 두 필드를 사용하여 워크 플로 작업 만 사용하고 목록에 ID가있는 항목이없는 경우 (항목이 삭제됨) 작업을 삭제할 수 있습니다.
2.) 종료 워크 플로우의 작업 삭제
워크 플로 상태를 가져오고 사용자가 중지 된 상태에서 작업을 삭제할 수 있습니다.
.int id = Convert.ToInt32(taskItem[SPBuiltInFieldId.WorkflowItemId]);
SPListItem item = list.GetItemById(id);
string workflowInstanceId = taskItem[SPBuiltInFieldId.WorkflowInstanceID].ToString();
SPWorkflow workflow = item.Workflows[new Guid(workflowInstanceId)];
If (workflow.StatusValue = SPWorkflowStatus.StoppedByUser)
{
taskItem.Delete();
}