문제

In c#, can you do something like this?

type typeOfInt = type(int);
int x = (typeOfInt)1;

Basically I want to store a type as a variable, so I can use the variable as a cast. I am trying to do this because in me parametric polymorphism function, a variable could be 1 of 2 types. Both types has the same methods I want to use, but it wont let me use it because it's of a variable type.

    public static void SetFolderOrderValue<T>(T item, int value, Report reportObject)
    {
        if (!item.Exists)
        {
            reportObject.Log("The folder \"" + item.Name + "\" does not exist.");
            return;
        }
        try
        {
            item.SetProperty(folderOrderProperty, value);
            item.Update();
        }
        catch (SPException ex)
        {
            reportObject.Log("An error occurred when saving changes to the folder \"" + item.Name + "\". Maybe due to concurrent changes from another user. Please try again after a minute.\n" + Report.GetErrorInfo(ex));
        }
        catch (Exception ex)
        {
            reportObject.Log("An error occured with \"" + item.Name + "\":\n" + Report.GetErrorInfo(ex));
        }
    }

If I can at least store the cast as a value, then I can just pass another boolean in the function saying which of the 2 types it is.

도움이 되었습니까?

해결책

You are talking about polymorphism, so use it.

If you have 2 types with same or common methods and you have a function or set of functions to act on them, define an interface that describes that set of methods shared between those 2 types and :

If the types you're talking about are named SPFile and SPFolder

public class SPFile : IMyNewInterface  {
   .....
}

public class SPFolder : IMyNewInterface  {
   ...
}

public static void SetFolderOrderValue<T>(T item, int value, 
              Report reportObject) where T : IMyNewInterface    {
     ...
}

다른 팁

No, you can't do this.

My answer assumes that Tigran's answer doesn't apply, maybe because you can't change the types in question.

The following would be one way to achieve what you want to do:

var varOfTypeOne = item as TypeOne;
if(varOfTypeOne != null)
{
    varOfTypeOne.CallMethod();
    return;
}
var varOfTypeTwo = item as TypeTwo;
if(varOfTypeTwo != null)
{
    varOfTypeTwo.CallMethod();
    return;
}

Another way would be to use the DLR via dynamic:

dynamic dynamicItem = item;
dynamicItem.CallMethod();

This will fail at runtime if the actual type of item doesn't define CallMethod, so you are basically losing compile time safety.

BTW: A void method that takes a generic parameter without constraint usually can simply be replaced by a method that is non-generic and has object as the parameter type.

Yes you can save the type but this is probably a code smell.

Read up on http://msdn.microsoft.com/en-us/library/system.object.gettype.aspx and http://msdn.microsoft.com/en-us/library/system.type.aspx

Here is an example

int x = 1;
var type = x.GetType();
Console.WriteLine(type);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top