Document directory
- Preface
- What's the problem with the generic attention type?
- Generic type does not support or implement non-generic type
Preface
Generic is a very easy-to-use technical example. Using generic can greatly increase the usage of different types, and vice versa, if you use. net developer, must be specific to the list <t> class. This is. net. The generic data t can be converted into any type, such as Int or string. The portable data is not safe, when Visual Studio is used, its intelliisense can also bring out-of-the-box information and accelerate program preparation. However, there are a lot of problems with generic scripting.
What's the problem with the generic attention type?
Generic functions are very useful, but after they are converted into objects, if you do not know the number of generic functions, there is no way to successfully compile them, as shown in the following example:
void Exec()
{
List<int> list = new List<int>();
Exec(list);
}
void Exec(object obj)
{
List <Object> List = OBJ as list <Object>; // token loss
}
It may occur when the hacker needs to copy the returned data from the object-type Qian, which may be converted into a prototype when the data is not synchronized, in the above example, why does the conversion from list <int> into list <Object> fail? Because in. when the net generic type is being used, the compiler will convert the generic type into a specific type, such as list <int> and change it to system. collections. generic. list '1 [[system. int32], while list <Object> changes to system. collections. generic. list '1 [[system. object], the two are different types of completion, even if the Int Is an upload object, there is no way to upload objects, as shown in the following example.
public class MyClassBase { }
public class MyClass : MyClassBase { }
void Exec()
{
List<MyClass> list = new List<MyClass>();
object obj = list;
}
void Exec(object obj)
{
List <myclassbase> List = OBJ as list <myclassbase>; // metadata loss
}
Although myclass is similar to myclassbase, it cannot be used because of the differences in the wildcard type, and list <myclass> except for converting to object or list <myclass>, is it possible to merge it into another one?
Generic type does not support or implement non-generic type
List<T> : IList<T> : IList
Because list <t> is actually ilist <t> and ilist <t> is actually ilist, list <t> can be merged into list. When you do not know or are likely to be too many, it can be processed as a list, as shown in the following example:
public class MyClassBase { }
public class MyClass : MyClassBase { }
void Exec()
{
List<MyClass> list = new List<MyClass>();
object obj = list;
}
void Exec(object obj)
{
IList list = obj as IList;
foreach (var item in list)
{
if (item is MyClassBase) { }
if (item is int) { }
if (item is string) { }
}
}
If you want to check it out. all the generic differences in. net will be accepted or implemented as non-generic differences, that is, the problem of fewer generic types, how can we implement it? For example:
// If it is a different implementation method
public class MyClass
{
public object Source { get; set; }
}
public class MyClass<T> : MyClass
{
Public new t source {Get; set;} // use new to change the object to restore t
}
// If the interface is used as an example
public interface MyInterface
{
object Source { get; set; }
}
public class MyInterface<T> : MyInterface
{
public T Source { get; set; }
Object myinterface. Source // implements a private interface
{
get
{
return this.Source;
}
set
{
this.Source = (T)value;
}
}
}
In this case, if you are not clear about the type of the generic regression data, you can also handle it as a general type.