本文整理汇总了C#中System.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# System.GetType方法的具体用法?C# System.GetType怎么用?C# System.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System
的用法示例。
在下文中一共展示了System.GetType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Invoke
public static object Invoke(System.Windows.Forms.Control obj, string methodName, params object[] paramValues)
{
Delegate del = null;
string key = obj.GetType().Name + "." + methodName;
Type tp;
lock (methodLookup)
{
if (methodLookup.Contains(key))
tp = (Type)methodLookup[key];
else
{
Type[] paramList = new Type[obj.GetType().GetMethod(methodName).GetParameters().Length];
int n = 0;
foreach (ParameterInfo pi in obj.GetType().GetMethod(methodName).GetParameters()) paramList[n++] = pi.ParameterType;
TypeBuilder typeB = builder.DefineType("Del_" + obj.GetType().Name + "_" + methodName, TypeAttributes.Class | TypeAttributes.AutoLayout | TypeAttributes.Public | TypeAttributes.Sealed, typeof(MulticastDelegate), PackingSize.Unspecified);
ConstructorBuilder conB = typeB.DefineConstructor(MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName, CallingConventions.Standard, new Type[] { typeof(object), typeof(IntPtr) });
conB.SetImplementationFlags(MethodImplAttributes.Runtime);
MethodBuilder mb = typeB.DefineMethod("Invoke", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig, obj.GetType().GetMethod(methodName).ReturnType, paramList);
mb.SetImplementationFlags(MethodImplAttributes.Runtime);
tp = typeB.CreateType();
methodLookup.Add(key, tp);
}
}
del = MulticastDelegate.CreateDelegate(tp, obj, methodName);
return obj.Invoke(del, paramValues);
}
示例2: Main_12_2_3
//Main_12_2_3
public static void Main_12_2_3()
{
//User user = new User { Name = "小王", Age = 27 };
var user = new { Name = "小王", Age = 27 };
var my = new { Name = "Wang", Emal = "[email protected]" };
//等效于User v = new User { Name = "小王", Age = 27 };
Console.WriteLine(user.Name);
Console.WriteLine(user.GetType());
var name = "小王";
var age = 27;
//等效于string v2 = "123";
Console.WriteLine(name.GetType());
Console.WriteLine(age.GetType());
//创建不同的Type
var v1 = new { Name = "Aero", Age = 27 };
var v2 = new { Name = "Emma", Age = 22 };
var v3 = new { Age = 27, Name = "Aero" };
Console.WriteLine(v1.Equals(v2));
Console.WriteLine(v1.Equals(v3));
Console.WriteLine(ReferenceEquals(v1.GetType(), v2.GetType()));
Console.WriteLine(ReferenceEquals(v1.GetType(), v3.GetType()));
}
示例3: EnumGridToolbar
/*--------------------------------------------------*/
/// <summary>
/// .
/// </summary>
/// <param name="selected"> . </param>
/// <returns> . </returns>
/*--------------------------------------------------*/
static public System.Enum EnumGridToolbar(System.Enum selected, int xCount)
{
#if UNITY_EDITOR
string[] toolbar = System.Enum.GetNames(selected.GetType());
System.Array values = System.Enum.GetValues(selected.GetType());
for (int i = 0; i < toolbar.Length; i++)
{
string toolname = toolbar[i];
toolname = toolname.Replace("_", " ");
toolbar[i] = toolname;
}
int selected_index = 0;
while (selected_index < values.Length)
{
if (selected.ToString() == values.GetValue(selected_index).ToString())
{
break;
}
selected_index++;
}
selected_index = UnityEngine.GUILayout.SelectionGrid(selected_index, toolbar, xCount);
return (System.Enum)values.GetValue(selected_index);
#else
return selected;
#endif
}
示例4: buildTreeLayer
void buildTreeLayer(System.Exception e, TreeViewItem parent)
{
String exceptionInformation = "\n\r\n\r" + e.GetType().ToString() + "\n\r\n\r";
parent.DisplayMemberPath = "Header";
parent.Items.Add(new TreeViewStringSet() { Header = "Type", Content = e.GetType().ToString() });
PropertyInfo[] memberList = e.GetType().GetProperties();
foreach (PropertyInfo info in memberList)
{
var value = info.GetValue(e, null);
if (value != null)
{
if (info.Name == "InnerException")
{
TreeViewItem treeViewItem = new TreeViewItem();
treeViewItem.Header = info.Name;
buildTreeLayer(e.InnerException, treeViewItem);
parent.Items.Add(treeViewItem);
}
else
{
TreeViewStringSet treeViewStringSet = new TreeViewStringSet() { Header = info.Name, Content = value.ToString() };
parent.Items.Add(treeViewStringSet);
exceptionInformation += treeViewStringSet.Header + "\n\r\n\r" + treeViewStringSet.Content + "\n\r\n\r";
}
}
}
_exceptionInformationList.Add(exceptionInformation);
}
示例5: AddAll
/// <summary>
/// Adds all of the elements of the "c" collection to the "target" collection.
/// </summary>
/// <param name="target">Collection where the new elements will be added.</param>
/// <param name="c">Collection whose elements will be added.</param>
/// <returns>Returns true if at least one element was added, false otherwise.</returns>
public static bool AddAll(System.Collections.ICollection target, System.Collections.ICollection c)
{
System.Collections.IEnumerator e = new System.Collections.ArrayList(c).GetEnumerator();
bool added = false;
//Reflection. Invoke "addAll" method for proprietary classes
System.Reflection.MethodInfo method;
try
{
method = target.GetType().GetMethod("addAll");
if (method != null)
added = (bool) method.Invoke(target, new System.Object[] {c});
else
{
method = target.GetType().GetMethod("Add");
while (e.MoveNext() == true)
{
bool tempBAdded = (int) method.Invoke(target, new System.Object[] {e.Current}) >= 0;
added = added ? added : tempBAdded;
}
}
}
catch (System.Exception ex)
{
throw ex;
}
return added;
}
示例6: Invoke
public static object Invoke(System.Windows.Forms.Control obj, string methodName, params object[] paramValues)
{
Delegate del = null;
DateTime now = DateTime.Now;
TimeSpan ts = now - lastCleanTime;
if(ts.TotalHours>5)
{
delLookup.Clear();
lastCleanTime = now;
}
string delKey = obj.GetType().Name+"."+methodName+obj.GetHashCode().ToString() + obj.ToString();
if(delLookup.ContainsKey(delKey))
{
del =(Delegate) delLookup[delKey];
}
else
{
string key = obj.GetType().Name + "." + methodName;
Type tp;
if (methodLookup.Contains(key))
{
tp = (Type)methodLookup[key];
}
else
{
Type objType = obj.GetType();
object[] signatures = new object[paramValues.Length + 1];
signatures[0] = methodName;
Array.Copy(paramValues, 0, signatures, 1, paramValues.Length);
MemberInfo[] mInfo = objType.FindMembers(MemberTypes.Method, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public, methodFilter, signatures);
if (mInfo.Length != 1) return null;
MethodInfo methInfo = (MethodInfo)mInfo[0];
Type[] paramList = new Type[methInfo.GetParameters().Length];
ParameterInfo[] paramInfos = methInfo.GetParameters();
int n = 0;
foreach (ParameterInfo pi in paramInfos)
{
paramList[n++] = pi.ParameterType;
}
TypeBuilder typeB = builder.DefineType("Del_" +
obj.GetType().Name + "_" + methodName,
TypeAttributes.Class | TypeAttributes.AutoLayout | TypeAttributes.Public | TypeAttributes.Sealed,
typeof(MulticastDelegate), PackingSize.Unspecified);
ConstructorBuilder conB = typeB.DefineConstructor(MethodAttributes.HideBySig | MethodAttributes.SpecialName
| MethodAttributes.RTSpecialName, CallingConventions.Standard,
new Type[] { typeof(object), typeof(IntPtr) });
conB.SetImplementationFlags(MethodImplAttributes.Runtime);
MethodBuilder mb = typeB.DefineMethod("Invoke",
MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig,
methInfo.ReturnType, paramList);
mb.SetImplementationFlags(MethodImplAttributes.Runtime);
tp = typeB.CreateType();
methodLookup.Add(key, tp);
}
del = MulticastDelegate.CreateDelegate(tp, obj, methodName);
delLookup.Add(delKey, del);
}
return obj.Invoke(del, paramValues);
}
示例7: Alert
/// <summary>
/// 弹出消息框,不会导致界面变形【推荐】
/// </summary>
/// <param name="message">消息内容</param>
/// <param name="page">当前页面对象,如this.Page</param>
public static void Alert(string message, System.Web.UI.Page page)
{
string js = @"<Script language='JavaScript'>alert('" + message + "');</Script>";
if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "alert"))
{
page.ClientScript.RegisterStartupScript(page.GetType(), "alert", js);
}
}
示例8: RegisterInstanceWithType
public void RegisterInstanceWithType()
{
var obj = new { ID = 1 };
ServiceContainer.Register (obj.GetType(), obj);
Assert.That (ServiceContainer.Resolve (obj.GetType ()), Is.EqualTo (obj));
}
示例9: Test3
public virtual void Test3()
{
var array1 = new[] {1, 2};
AssertEquals(OdbType.Array, OdbType.GetFromClass(array1.GetType()));
AssertEquals(OdbType.Integer, OdbType.GetFromClass(array1.GetType()).SubType);
var array2 = new[] {"1", "2"};
AssertEquals(OdbType.Array, OdbType.GetFromClass(array2.GetType()));
AssertEquals(OdbType.String, OdbType.GetFromClass(array2.GetType()).SubType);
}
示例10: GetDictionaryValueOrDefaultReturnsArrayIfArrayDesiredFromDictionaryKeyFound
public void GetDictionaryValueOrDefaultReturnsArrayIfArrayDesiredFromDictionaryKeyFound()
{
var expected = new[] { "Cameron" };
var dictionary = new Dictionary<string, JToken> { { "fields", JToken.Parse("[ \"" + expected[0] + "\" ]") } };
var actual = MemberProjectionExpressionVisitor.GetDictionaryValueOrDefault(dictionary, "fields", expected.GetType());
Assert.IsType(expected.GetType(), actual);
Assert.Equal(expected, actual);
}
示例11: DesignRule
public static void DesignRule(System.Workflow.ComponentModel.Activity activity, bool UseProvider)
{
RuleSet ruleSet = null;
//MessageBox.Show(activity.UserData[0].ToString());
//MessageBox.Show(activity.GetType().ToString());
string ruleName = activity.UserData[0].ToString();
//load ruleset
try
{
ruleSet = RuleSetManagerFactory.CreateRuleSetManager(RuleDesigner.RemotingUrl).getRuleSetDesign("", "", ruleName);
}
catch (Exception e)
{
MessageBox.Show("Error deserializing file: " + ruleName + e.Message);
}
if (ruleSet == null)
{
MessageBox.Show("RuleSet is null");
ruleSet = new RuleSet(ruleName);
}
RuleSetDialog ruleSetDialog = null;
if (UseProvider)
{
TypeProvider provider = new TypeProvider(null);
AssemblyName[] ass = activity.GetType().Assembly.GetReferencedAssemblies();
provider.AddAssembly(activity.GetType().Assembly);
FillWithAssemblies(provider, ass);
ruleSetDialog = new RuleSetDialog(activity.GetType(), provider, ruleSet);
}
else
{
ruleSetDialog = new RuleSetDialog(activity, ruleSet);
}
var result = ruleSetDialog.ShowDialog();
// Only update the .rules file if the OK is pressed
if (result == DialogResult.OK)
{
ruleSet = ruleSetDialog.RuleSet;
try
{
Console.WriteLine(ruleName);
RuleSetManagerFactory.CreateRuleSetManager(RuleDesigner.RemotingUrl).saveRuleSetDesign("", "", ruleName, ruleSet);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
}
示例12: EvaluateEnum
/// <summary>
/// Prints all info about passed enum
/// </summary>
/// <exception cref="IOException">I/O exception occured. </exception>
/// <exception cref="ArgumentNullException">Parameter <paramref name="format" /> has null value. </exception>
/// <exception cref="FormatException">Format specification set in parameter <paramref name="format" /> is not acceptable. </exception>
/// <exception cref="ArgumentException">Parameter value for <paramref name="enumType" /> is not Enum <see cref="T:System.Enum" />. </exception>
/// <exception cref="InvalidOperationException">Method is called in reflection context intended only for reflection.
/// -or-<paramref name="enumType" /> — is a build type loaded in context suited only for reflection.</exception>
private static void EvaluateEnum (System.Enum e) {
Console.WriteLine ("*** Info about {0} ***", e.GetType ().Name);
Console.WriteLine ("Underlying storage type: {0}", Enum.GetUnderlyingType (e.GetType ()));
Array enumData = Enum.GetValues (e.GetType ());
foreach (var eData in enumData) {
Console.WriteLine ("Name - {0}, Value - {0:D}", eData);
}
Console.WriteLine ();
}
示例13: EvaluateEnum
static void EvaluateEnum(System.Enum e)
{
Console.WriteLine("information about {0}", e.GetType().Name);
Console.WriteLine("underlying storage type: {0}", Enum.GetUnderlyingType(e.GetType()));
Array enumData = Enum.GetValues(e.GetType());
Console.WriteLine("this enum has {0} members", enumData.Length);
for (int i=0; i<enumData.Length; i++)
{
Console.WriteLine("name: {0}, Value: {0:D}", enumData.GetValue(i));
}
}
示例14: GenerateReport
public static void GenerateReport(string FileName, ReportDocument rpt, System.Web.UI.Control ClientScriptBlockControl, ExportFormatType pvtExportFormatType)
{
try
{
ExportOptions exportop = new ExportOptions();
DiskFileDestinationOptions dest = new DiskFileDestinationOptions();
string strFileExtensionName = ".pdf";
switch (pvtExportFormatType)
{
case ExportFormatType.PortableDocFormat: strFileExtensionName = ".pdf"; exportop.ExportFormatType = ExportFormatType.PortableDocFormat; break;
case ExportFormatType.WordForWindows: strFileExtensionName = ".doc"; exportop.ExportFormatType = ExportFormatType.WordForWindows; break;
case ExportFormatType.Excel: strFileExtensionName = ".xls"; exportop.ExportFormatType = ExportFormatType.Excel; break;
}
string strPath = System.Web.HttpContext.Current.Server.MapPath(@"\retailplus\temp\");
string strFileName = FileName + "_" + DateTime.Now.ToString("yyyyMMddhhmmssff") + strFileExtensionName;
if (System.IO.File.Exists(strPath + strFileName))
System.IO.File.Delete(strPath + strFileName);
dest.DiskFileName = strPath + strFileName;
exportop.DestinationOptions = dest;
exportop.ExportDestinationType = ExportDestinationType.DiskFile;
rpt.Export(exportop); //rpt.Close(); rpt.Dispose();
// remove the error
if (pvtExportFormatType == ExportFormatType.WordForWindows || pvtExportFormatType == ExportFormatType.Excel || pvtExportFormatType == ExportFormatType.PortableDocFormat)
{
// the maximum report processing jobs limit configured by your system administrator has been reached.
rpt.Close(); rpt.Dispose();
}
if (pvtExportFormatType == ExportFormatType.PortableDocFormat)
{
string newWindowUrl = Constants.ROOT_DIRECTORY + "/temp/" + strFileName;
string javaScript = "window.open('" + newWindowUrl + "');";
System.Web.UI.ScriptManager.RegisterClientScriptBlock(ClientScriptBlockControl, ClientScriptBlockControl.GetType(), "openwindow", javaScript, true);
}
else
{
string newWindowUrl = Constants.ROOT_DIRECTORY + "/temp/" + strFileName;
string javaScript = "window.open('" + newWindowUrl + "','_self');";
System.Web.UI.ScriptManager.RegisterClientScriptBlock(ClientScriptBlockControl, ClientScriptBlockControl.GetType(), "openwindow", javaScript, true);
//System.Diagnostics.Process p = new System.Diagnostics.Process();
//p.StartInfo.FileName = System.Web.HttpContext.Current.Server.MapPath(Constants.ROOT_DIRECTORY + "/temp/" + strFileName);
//p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
//p.Start();
}
}
catch (Exception ex) { throw ex; }
}
示例15: RawObjStats
RawObjStats( System.Object obj )
{
RawObjCount tmpNode = FindRawObjNode( obj.GetType() );
if( tmpNode == null )
{
tmpNode = new RawObjCount();
tmpNode.m_classType = obj.GetType();
m_rawObjCounts.Add( tmpNode );
}
tmpNode.m_objs.Add( obj );
}