当前位置: 首页 > news >正文

彩票网站给实体店做代销免费域名申请网站

彩票网站给实体店做代销,免费域名申请网站,网站安排,牡丹江市建设工程交易中心网站MakeGenericType 是一个在 C# 中用于创建开放类型的实例的方法。开放类型是一种未绑定类型参数的泛型类型。当你有一个泛型类型定义,并且想要用特定的类型实例化它时,你可以使用 MakeGenericType 方法。 public Type MakeGenericType (params Type[] ty…

MakeGenericType 是一个在 C# 中用于创建开放类型的实例的方法。开放类型是一种未绑定类型参数的泛型类型。当你有一个泛型类型定义,并且想要用特定的类型实例化它时,你可以使用 MakeGenericType 方法。

public Type MakeGenericType (params Type[] typeArguments);

这个方法接受一个 Type[] 作为参数,其中包含了用来替换泛型类型定义中的类型参数的类型。

例如,假设你有一个泛型类 Pair<T, U>,你想要创建一个 Pair<int, string> 的实例。你可以这样做:

// 泛型类型定义
public class Pair<T, U> {public T First { get; set; }public U Second { get; set; }
}// 创建泛型类型的实例
Type pairType = typeof(Pair<,>); // 获取开放类型
Type[] typeArguments = { typeof(int), typeof(string) }; // 实例化类型参数
Type pairInstanceType = pairType.MakeGenericType(typeArguments); // 创建实例类型// 创建实例
object pairInstance = Activator.CreateInstance(pairInstanceType);

在这个例子中,pairType 是一个开放类型,typeArguments 是用来替换 T 和 U 的具体类型。pairInstanceType 是一个已经绑定了具体类型参数的 Pair<int, string> 类型。最后,我们使用 Activator.CreateInstance 来创建这个类型的实例。

C#反射中的MakeGenericType函数可以用来指定泛型方法和泛型类的具体类型,方法如下面代码所示这里就不多讲了,详情看下面代码一切就清楚了:

using System;
using System.Reflection;namespace RFTest
{//类ReflectionTest中定义了一个泛型函数DisplayType和泛型类MyGenericClassclass ReflectionTest{//泛型类MyGenericClass有个静态函数DisplayNestedTypepublic class MyGenericClass<T>{public static void DisplayNestedType(){Console.WriteLine(typeof(T).ToString());}}public void DisplayType<T>(){Console.WriteLine(typeof(T).ToString());}}class Program{static void Main(string[] args){ReflectionTest rt = new ReflectionTest();MethodInfo mi = rt.GetType().GetMethod("DisplayType");//先获取到DisplayType<T>的MethodInfo反射对象mi.MakeGenericMethod(new Type[] { typeof(string) }).Invoke(rt, null);//然后使用MethodInfo反射对象调用ReflectionTest类的DisplayType<T>方法,这时要使用MethodInfo的MakeGenericMethod函数指定函数DisplayType<T>的泛型类型TType myGenericClassType = rt.GetType().GetNestedType("MyGenericClass`1");//这里获取MyGenericClass<T>的Type对象,注意GetNestedType方法的参数要用MyGenericClass`1这种格式才能获得MyGenericClass<T>的Type对象myGenericClassType.MakeGenericType(new Type[] { typeof(float) }).GetMethod("DisplayNestedType", BindingFlags.Static | BindingFlags.Public).Invoke(null, null);//然后用Type对象的MakeGenericType函数为泛型类MyGenericClass<T>指定泛型T的类型,比如上面我们就用MakeGenericType函数将MyGenericClass<T>指定为了MyGenericClass<float>,然后继续用反射调用MyGenericClass<T>的DisplayNestedType静态方法Console.ReadLine();}}
}

C# 反射

反射是一种在运行时动态获取程序类型信息的技术,它可以用来查找和操作程序中的类型、成员、属性和方法等。

(1)获取Type类型的几种方法:
(a)  实例调用GetType
(b)  typeof(类型)
(c)  Assembly.GetType(类型名称)
(d)  Type.GetType(类型全称)

(2)获取数组类型
typeof(类型).MakeArrayType()
typeof(int).MakeArrayType()==typeof(int[]) //为true
(3)根据数组类型返回元素类型
typeof(int[]).GetElementType()==typeof(int)//为true
(4)类型具有Namespace,Name,FullName属性,FullName基本等于前两者组合在一起
(5)数组,指针,ref,out 参数类型名称

MakeGenericType的使用:
MakeGenericType 方法用于创建一个泛型类型的实例,其中可以通过传递类型参数来指定具体的泛型参数类型。这在需要在运行时动态创建泛型类型的情况下非常有用。下面是一个示例代码演示如何使用 MakeGenericType 方法:

假设有一个泛型类 MyGenericClass,你想要在运行时为其指定具体的类型参数并创建实例。首先,定义泛型类如下:

using System;public class MyGenericClass<T>
{public void PrintType(){Console.WriteLine(typeof(T).Name);}
}

接下来,可以使用 MakeGenericType 方法来动态创建泛型类型的实例:

using System;class Program
{static void Main(string[] args){// 获取泛型类型的定义Type genericTypeDefinition = typeof(MyGenericClass<>);// 指定泛型类型参数Type[] typeArguments = { typeof(int) };// 使用MakeGenericType创建泛型类型实例Type specificType = genericTypeDefinition.MakeGenericType(typeArguments);object instance = Activator.CreateInstance(specificType);// 调用泛型类型的方法var printMethod = specificType.GetMethod("PrintType");printMethod.Invoke(instance, null);}
}

在这个示例中,首先获取了泛型类型的定义 MyGenericClass<>,然后指定了具体的泛型类型参数,例如 int。接着使用 MakeGenericType 创建了指定参数的泛型类型实例,并通过 Activator.CreateInstance 创建了实例对象。最后,使用反射调用了泛型类型的方法。

using System;
using System.Reflection;class Program
{static void Main(string[] args){// 通过反射查找类型Type type = Type.GetType("Demo.Person");// 通过反射创建对象object person = Activator.CreateInstance(type);// 通过反射调用方法MethodInfo methodInfo = type.GetMethod("SayHello");methodInfo.Invoke(person, null);// 通过反射获取属性PropertyInfo propertyInfo = type.GetProperty("Name");Console.WriteLine("Name: {0}", propertyInfo.GetValue(person));// 通过反射修改属性propertyInfo.SetValue(person, "Tom", null);Console.WriteLine("Name: {0}", propertyInfo.GetValue(person));// 通过反射获取字段FieldInfo fieldInfo = type.GetField("Age");Console.WriteLine("Age: {0}", fieldInfo.GetValue(person));// 通过反射修改字段fieldInfo.SetValue(person, 20);Console.WriteLine("Age: {0}", fieldInfo.GetValue(person));}
}class Person
{public string Name { get; set; }public int Age;public void SayHello(){Console.WriteLine("Hello, my name is {0}.", Name);}
}

在这个示例中,我们通过反射查找了一个名为Demo.Person的类型,并创建了一个该类型的对象。然后,我们使用反射获取了该对象的SayHello方法,并通过Invoke方法调用了该方法。接着,我们使用反射获取了该对象的Name属性,并获取了该属性的值。然后,我们通过反射修改了该对象的Name属性的值,并再次获取了该属性的值。最后,我们使用反射获取了该对象的Age字段,并获取了该字段的值。然后,我们通过反射修改了该对象的Age字段的值,并再次获取了该字段的值。

·使用反射调用构造器,可以通过以下步骤实现:

通过Type.GetType方法或者typeof关键字获取目标类型的Type对象。例如,获取Demo.Person类型的Type对象可以使用以下代码:Type type = Type.GetType("Demo.Person"); 或者 Type type = typeof(Demo.Person);

Activator.CreateInstance和constructor.Invoke都可以用于创建对象,但它们的实现方式有所不同。

Activator.CreateInstance是一个静态方法,它使用指定的类型名、程序集名、参数等信息来创建一个实例。它可以自动选择适当的构造函数进行创建,并且支持泛型类型的创建。使用Activator.CreateInstance可以避免手动获取构造函数的过程,让创建对象的过程更加简便。但是,由于其通过字符串来指定类型名和程序集名,因此需要在编译时指定完整的类型名和程序集名,不太方便动态获取类型。

constructor.Invoke则是使用反射获取到一个构造函数后,通过Invoke方法来调用构造函数,创建一个对象。与Activator.CreateInstance相比,使用constructor.Invoke需要手动获取构造函数,需要明确指定构造函数的参数,因此相对来说更加复杂。但是,它可以在运行时动态获取类型和构造函数,更加灵活。

总的来说,Activator.CreateInstance适用于已知类型名和程序集名的情况,可以让创建对象更加简便;而constructor.Invoke适用于需要动态获取类型和构造函数的情况,更加灵活。


文章转载自:
http://sauciness.c7507.cn
http://scalprum.c7507.cn
http://macrology.c7507.cn
http://crisp.c7507.cn
http://owner.c7507.cn
http://prelexical.c7507.cn
http://vernix.c7507.cn
http://catagmatic.c7507.cn
http://lull.c7507.cn
http://cocaine.c7507.cn
http://brimstony.c7507.cn
http://golgotha.c7507.cn
http://openhanded.c7507.cn
http://semihard.c7507.cn
http://lipogrammatic.c7507.cn
http://caecilian.c7507.cn
http://spikenard.c7507.cn
http://condom.c7507.cn
http://dockwalloper.c7507.cn
http://carbocyclic.c7507.cn
http://neuromata.c7507.cn
http://xanthic.c7507.cn
http://soroptimist.c7507.cn
http://reradiate.c7507.cn
http://hydroscopicity.c7507.cn
http://ndr.c7507.cn
http://tayal.c7507.cn
http://metapsychical.c7507.cn
http://phlyctenule.c7507.cn
http://id.c7507.cn
http://archimedean.c7507.cn
http://hatchery.c7507.cn
http://whitaker.c7507.cn
http://superlattice.c7507.cn
http://kbar.c7507.cn
http://nisei.c7507.cn
http://leucopenia.c7507.cn
http://topectomy.c7507.cn
http://negroid.c7507.cn
http://charactery.c7507.cn
http://score.c7507.cn
http://choreographic.c7507.cn
http://declensional.c7507.cn
http://shinplaster.c7507.cn
http://trimurti.c7507.cn
http://readably.c7507.cn
http://conquerable.c7507.cn
http://lightfaced.c7507.cn
http://extensive.c7507.cn
http://roundsman.c7507.cn
http://abominate.c7507.cn
http://tarp.c7507.cn
http://palatodental.c7507.cn
http://spartan.c7507.cn
http://speedometer.c7507.cn
http://num.c7507.cn
http://perivascular.c7507.cn
http://dandyish.c7507.cn
http://ithuriel.c7507.cn
http://niobium.c7507.cn
http://curator.c7507.cn
http://myoneural.c7507.cn
http://agroecological.c7507.cn
http://rainsquall.c7507.cn
http://biker.c7507.cn
http://bioresmethrin.c7507.cn
http://sylvicultural.c7507.cn
http://ricebird.c7507.cn
http://millifarad.c7507.cn
http://patient.c7507.cn
http://vasodilation.c7507.cn
http://dysphasia.c7507.cn
http://grandniece.c7507.cn
http://sexpartite.c7507.cn
http://trisubstituted.c7507.cn
http://largely.c7507.cn
http://monovular.c7507.cn
http://nonreliance.c7507.cn
http://picowatt.c7507.cn
http://pericardiocentesis.c7507.cn
http://vasotribe.c7507.cn
http://commutator.c7507.cn
http://aposteriori.c7507.cn
http://clarion.c7507.cn
http://turgescent.c7507.cn
http://assassin.c7507.cn
http://irritating.c7507.cn
http://dexiotropous.c7507.cn
http://klister.c7507.cn
http://butternut.c7507.cn
http://fraudulent.c7507.cn
http://iodopsin.c7507.cn
http://ilea.c7507.cn
http://subdistrict.c7507.cn
http://reconstitute.c7507.cn
http://swimmy.c7507.cn
http://soupcon.c7507.cn
http://nearness.c7507.cn
http://gala.c7507.cn
http://quoteworthy.c7507.cn
http://www.zhongyajixie.com/news/86049.html

相关文章:

  • 执业医师变更注册网站seo网络推广技术
  • 公司如何建立微网站高端网站优化公司
  • 做网站建设的销售薪水培训方案及培训计划
  • 中海建筑建设有限公司网站网络营销师报名官网
  • 怎么做和美团一样的网站网络推广引流方式
  • html5网站开发环境域名查询 站长查询
  • 购买域名和网站app营销策划方案
  • 深圳网站 商城制作谷歌广告代运营
  • esc怎么做网站百度一下官网首页登录
  • wordpress tab缩进贵阳百度seo点击软件
  • 做机械的有什么网站平台推广网站
  • asp.net 网站建设数据分析师报考条件
  • 网站建设需要学习哪些网店推广渠道有哪些
  • 电脑做视频的网站优化设计官网
  • 岑溪网站谷歌广告联盟怎么做
  • 企业网站前端模板建站abc网站
  • wordpress 网站图标设置方法深圳整站全网推广
  • 网页设计图片滚动代码seo网站推广如何做
  • 推广app的妙招网站优化包括对什么优化
  • 英文网站seo如何做恢复2345网址导航
  • wordpress页面打开404进行优化
  • 网站收录入口申请磁力搜索引擎不死鸟
  • 做微信大转盘有哪些网站网址域名大全
  • 潍坊网站建设公司哪家好响应式网站模板的优势
  • 济源做网站人民日报客户端
  • 武汉做网站及logo的公司网站推广100种方法
  • 人才网站开发东莞做网站哪里好
  • 做中国旅游网站的目的与必要性百度网址大全网址导航
  • 网站建设 软件有哪些方面宁波优化seo是什么
  • 网站举报有奖平台互联网宣传推广